如何检查密码和确认密码在AngularJS中也是一样

How to check Password and Confirm Password Same in AngularJS

本文关键字:密码 一样 AngularJS 确认 何检查 检查      更新时间:2023-09-26

嗨,谁能帮助我如何在AngularJS中验证密码和确认密码字段

有很多方法可以做到这一点。一种方法是使用指令

<input type="password" name="confirmPassword" 
    ng-model="registration.user.confirmPassword"
    required 
    compare-to="registration.user.password" />
<div ng-messages="registrationForm.confirmPassword.$error"
  ng-messages-include="messages.html"></div>

//指令

var compareTo = function() {
  return {
    require: "ngModel",
    scope: {
        otherModelValue: "=compareTo"
    },
    link: function(scope, element, attributes, ngModel) {
        ngModel.$validators.compareTo = function(modelValue) {
            return modelValue == scope.otherModelValue;
        };
        scope.$watch("otherModelValue", function() {
            ngModel.$validate();
        });
    }
  };
};
module.directive("compareTo", compareTo);
参考