angularjs密码确认不显示确认消息

时间:2017-11-14 09:04:08

标签: angularjs

请帮助我,为什么我无法显示确认信息

angular.module('MyApp')
    .directive('pwCheck', function () {
        return {
            restrict: 'A', // only activate on element attribute
            require: '?ngModel',

            link: function (scope, elem, attr, ngModel) {
                var val2 = "";
                scope.$watch(attr.ngModel, function () {
                    validate();
                })
                attr.$observe('pwCheck', function (val) {
                    validate();
                });
                var validate = function () {
                    var val1 = ngModel.$viewValue;
                    var val2 = attr.pwCheck;
                    ngModel.$setValidity('passwordVerify', val1 === val2);

HtmlCode

这里我试图验证Emp_PasswordEmp_Cpsw的{​​{1}}使用EmpCpsw指令。指令工作正常但确认消息不弹出

<b>EnterPassword</b> :<input type="password" name="Emp_Password" ng-model="psw1" pw-check="{{psw2}}" required />

                <span class="error" ng-show="pwCheck">Password not match</span>
                <b>c_password</b><input type="password" name="Emp_Cpsw" ng-model="psw2" pw-check required pw-check="{{psw1}}" />           
                <span class="error" ng-show="pwCheck">Password not match</span>

1 个答案:

答案 0 :(得分:0)

您添加了名为setValidity的{​​{1}},因此应在passwordVerify中使用相同的名称。

view

所以,使用

ngModel.$setValidity('passwordVerify', val1 === val2);