AngularJS ng已禁用,因为它应该被激活

时间:2014-10-02 20:49:11

标签: angularjs

我有一个代码应该在以下时间禁用按钮:registerForm.$invalid || form.password != form.passwordBis,这应该意味着:“如果表单无效或者如果'$ scope.form.password'变量不是,则禁用按钮等于'$ scope.form.passwordBis'。“,不是吗?

所以这里我有一个代码,如果两个密码匹配,则激活按钮。哪个不应该发生,因为第一个条件仍然是错误的!

HTML:

<div ng-app="app" ng-controller="formCtrl">
    <form name="registerForm">
        <div class="header">
            <h2>Create your account</h2>
        </div>
        <div class="form-group">
            <div class="title">First Name</div>
            <input type="text" name="firstName" ng-required class="form-control" ng-class="{empty:form.firstName.length==0}" ng-model="form.firstName" required></input>
        </div>
        <div class="form-group">
            <div class="title">Last Name</div>
            <input type="text" name="lastName" ng-required class="form-control" ng-class="{empty:form.lastName.length==0}" ng-model="form.lastName" required></input>
        </div>
        <div class="form-group">
            <div class="title">Email</div>
            <input type="email" name="email" ng-required class="form-control" ng-class="{empty:form.email.length==0}" ng-model="form.email" required></input>
        </div>
        <div class="form-group">
            <div class="title">Password</div>
            <input type="password" name="password" class="form-control" ng-class="{empty:form.password.length==0}" ng-model="form.password" required></input>
            <input type="password" name="passwordBis" class="form-control" ng-class="{empty:form.passwordBis.length==0}" ng-model="form.passwordBis" minlength="6" required></input>
        </div>
        <div ng-if="form.password != form.passwordBis" class="error">Passwords doesn't match.</div>
        <div class="form-group">
            <button class="btn btn-success" ng-click="register()" ng-disabled="registerForm.$invalid || form.password != form.passwordBis">Register</button><br/>
        </div>
    </form>
</div>

JS:

var app = angular.module("app", []);

app.controller("formCtrl", function($scope) {
    $scope.form = {
            firstName: "",
            lastName: "",
            email: "",
            password: "",
            passwordBis: ""
    };
});

你可以测试它:JSFiddle

我错过了什么?

1 个答案:

答案 0 :(得分:2)

从表单元素中删除ng-required属性。

对于表单元素,您应该使用ng-requiredrequired,但不能同时使用两者。 ng-required需要一个需要求值为true的表达式,以便根据需要标记表单元素,而required不需要任何值。有关详细说明,请参阅https://stackoverflow.com/a/16648851/1237995