Javascript 用于检查密码的angularjs指令已重复,不起作用

Javascript 用于检查密码的angularjs指令已重复,不起作用,javascript,angularjs,angularjs-directive,angular-ui-router,Javascript,Angularjs,Angularjs Directive,Angular Ui Router,为了使AngularJS指令起作用,我遵循本教程: 在注册表单中,名为repeat password的指令检查表单中输入的两封电子邮件是否对应: <div class="form-group" ng-class="{ 'has-success' : signupForm.confirmPassword.$valid && signupForm.confirmPassword.$dirty, 'has-error' : signupForm.confirmPassword.$

为了使AngularJS指令起作用,我遵循本教程:

在注册表单中,名为repeat password的指令检查表单中输入的两封电子邮件是否对应:

<div class="form-group" ng-class="{ 'has-success' : signupForm.confirmPassword.$valid && signupForm.confirmPassword.$dirty, 'has-error' : signupForm.confirmPassword.$invalid && signupForm.confirmPassword.$dirty }">
    <input class="form-control input-lg" type="password" name="confirmPassword" ng-model="confirmPassword" repeat-password="password" placeholder="Confirm Password" required>

    <div class="help-block text-danger my-special-animation" ng-if="signupForm.confirmPassword.$dirty"ng-messages="signupForm.confirmPassword.$error">
       <div ng-message="required">You must confirm password.</div>
       <div ng-message="repeat">Passwords do not match.</div>
    </div>
 </div>

 <button type="submit" ng-disabled="signupForm.$invalid" class="btn btn-lg btn-block btn-primary">Create Account</button>

我不知道它是否有用,但我使用的是AngularJS v1.3.0-beta.15和Angular-ui.router 0.2.10。

这里是fieldMatch指令的通用版本,带有对其他SO问题的注释和参考。它已经用1.2和1.3测试版进行了测试

.directive('fieldMatch', function () {
    return {
        restrict: 'A',
        scope: true,
        require: 'ngModel',
        link: function (scope, elem, attrs, ngModel) {
            var checkFieldMatch = function () {
                // WARNING - FIXME:
                // when a validator fails (returns false), then the underlying model value is set to undefined
                //
                // https://stackoverflow.com/questions/24692775
                // https://stackoverflow.com/questions/24385104
                //
                // => solution: use ngModel.$viewValue instead of ngModel.$modelValue

                var fieldMatch = scope.$eval(attrs.dsFieldMatch),
                    value = ngModel.$modelValue || ngModel.$viewValue;

                // If fieldMatch or ngModel.$viewValue is defined,
                // they should match
                if (fieldMatch || value) {
                    return fieldMatch === value;
                }
                return true;
            };

            scope.$watch(checkFieldMatch, function (n) {
                //set the form control to valid if both
                //passwords are the same, else invalid
                ngModel.$setValidity('fieldMatch', n);
            });
        }
    };
});
请参见以下两个其他问题:


你测试过我提供的解决方案吗?如果对您有效,请接受“关闭”您的问题的答案。如果您的问题没有解决,请告诉我如何帮助您
.directive('fieldMatch', function () {
    return {
        restrict: 'A',
        scope: true,
        require: 'ngModel',
        link: function (scope, elem, attrs, ngModel) {
            var checkFieldMatch = function () {
                // WARNING - FIXME:
                // when a validator fails (returns false), then the underlying model value is set to undefined
                //
                // https://stackoverflow.com/questions/24692775
                // https://stackoverflow.com/questions/24385104
                //
                // => solution: use ngModel.$viewValue instead of ngModel.$modelValue

                var fieldMatch = scope.$eval(attrs.dsFieldMatch),
                    value = ngModel.$modelValue || ngModel.$viewValue;

                // If fieldMatch or ngModel.$viewValue is defined,
                // they should match
                if (fieldMatch || value) {
                    return fieldMatch === value;
                }
                return true;
            };

            scope.$watch(checkFieldMatch, function (n) {
                //set the form control to valid if both
                //passwords are the same, else invalid
                ngModel.$setValidity('fieldMatch', n);
            });
        }
    };
});