我的脚本未禁用textboxes-AngularJS

时间:2018-07-10 08:05:27

标签: javascript angularjs

用于启用和禁用文本框的代码段

var app = angular.module('DemoApp', []);
app.controller('myController', function($scope) {
  $scope.pass = true;
  $scope.email = true;
  $scope.age = true;
  $scope.stat = true;
  $scope.submit = true;
  var name = $scope.uname;
  var pss = $scope.pass;
  var email = $scope.email;
  var age = $scope.age;
  var status = $scope.stat;
  if (name.length != 0) {
    $scope.pass = false;
  } else if (pss.length != 0) {
    $scope.email = false;
  } else if (email.length != 0) {
    $scope.age = false;
  } else if (age.length != 0) {
    $scope.stat = false;
  } else if (status.length != 0) {
    $scope.submit = false;
  }
})
<head>
  <script src="/lib/angular.min.js"></script>
  <script src="js/app.js"></script>
</head>

<body ng-app="DemoApp" ng-controller="myController">

  <fieldset>
    <legend>User Login</legend>

    <dl>
      <dt>Username</dt>
      <dt><input type="text" ng-model="uname" ng-change="myController"></dt>
    </dl>
    <dl>
      <dt>Password</dt>
      <dt><input type="password" ng-model="pass" ng-change="myController" ></dt>
    </dl>
    <dl>
      <dt>E-mail</dt>
      <dt><input type="email" ng-model="email" ng-change="myController"></dt>
    </dl>
    <dt>Age</dt>
    <dt><input type="text" ng-model="age" ng-change="myController"></dt>
    <dl>
      <dt>Status</dt>
      <dt><input type="text" ng-model="stat" ng-change="myController"></dt>
    </dl>
    <dl>
      <dt></dt>
      <dt><input type="submit" ng-model="submit" ng-change="myController" value="submit"></dt>
    </dl>

  </fieldset>
</body>

</html>

var app= angular.module('DemoApp',[]);行中说:

未定义角度

因为我已经包含了所有需要的库。
此应用程序的目的是禁用所有文本框,并在用户一张一张填写表单时动态启用。

1 个答案:

答案 0 :(得分:0)

尝试一下

angular.module('DemoApp', [])
  .controller('myController', ['$scope', function($scope) {

    $scope.pass = true;
    $scope.email = true;
    $scope.age = true;
    $scope.stat = true;
    $scope.submit = true;
    var name = $scope.uname;
    var pss = $scope.pass;
    var email = $scope.email;
    var age = $scope.age;
    var status = $scope.stat;

    if (name.length != 0) {
      $scope.pass = false;
    } else if (pss.length != 0) {
      $scope.email = false;
    } else if (email.length != 0) {
      $scope.age = false;
    } else if (age.length != 0) {
      $scope.stat = false;
    } else if (status.length != 0) {
      $scope.submit = false;
    }
  }]);