【发布时间】:2015-09-17 09:50:04
【问题描述】:
您好,谁能帮助我在 AngularJS 中验证密码和确认密码字段
【问题讨论】:
-
请仔细阅读 Stackoverflow 在how to ask questions 上的指南以了解未来的问题。
-
请在此处发布任何问题之前进行一些初步研究。无论如何,下面的答案会对您有所帮助。
标签: javascript angularjs
您好,谁能帮助我在 AngularJS 中验证密码和确认密码字段
【问题讨论】:
标签: javascript 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);
【讨论】: