ng-disable不适用于隐藏提交按钮

时间:2016-06-27 08:38:18

标签: angularjs

您好我的提交按钮的ng-disabled有问题。这是我的代码:

<div class="container">
<form class="form-login" role="form" name="form">
  <h2 class="form-login-heading">Sign up</h2>
  <br />
  <input type="text" class="form-control" placeholder="Username" name="username" data-ng-model="registration.username" ng-minlength="5" ng-maxlength="20" ng-pattern="/^[A-z][A-z0-9]*$/" required />
  <span ng-show="form.username.$error.required && form.username.$dirty">required</span>
  <br />

  <input type="password" id="password" class="form-control" placeholder="Password" name="password" data-ng-model="registration.plainPassword" ng-minlength="8" ng-maxlength="20" ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])/" required />
  <span ng-show="form.password.$error.required && form.password.$dirty">required</span>
  <br />

  <input type="password" id="password_c" class="form-control" placeholder="Confirm password" name="password_c" data-ng-model="registration.password_c" valid-password-c required />
  <br />

  <input type="email" id="email" class="form-control" placeholder="email" name="email" data-ng-model="registration.email" required>
  <span ng-show="form.email.$error.required && form.email.$dirty">required</span>
  <br />

  <button class="btn btn-lg btn-info btn-block" type="submit" ng-disabled="form.$invalid" data-ng-click="signUp()">Submit</button>
  <div data-ng-hide="message == ''" data-ng-class="(savedSuccessfully) ? 'alert alert-success' : 'alert alert-danger'">
    {{message}}
  </div>
</form>

我尝试过ng-disabled =“!form。$ valid”,但代码仍无效。控制台不显示任何错误。有人能帮我吗?谢谢

2 个答案:

答案 0 :(得分:1)

我们解决了这个问题,对@Silvinus来说,他救了我的生命和时间 问题出在我的指令中,用于密码确认。我忘了添加回报。这就是它的外观

.directive('validPasswordC', function () {
return {
    require: 'ngModel',
    link: function (scope, elm, attrs, ctrl) {
        ctrl.$parsers.unshift(function (viewValue, $scope) {
            var noMatch = viewValue != scope.form.password.$viewValue
            ctrl.$setValidity('noMatch', !noMatch);
            return viewValue;    //this is what i forget to add
        });
    }
};
});

答案 1 :(得分:0)

我复制了你的代码,它适合我。也许指令&#39; valid-password-c&#39;如果错了。