问问题
614 次
1 回答
0
也许它可以帮助你。我创建了这个指令来验证表单中两个值的相等性。指令检查相等性并将手动验证结果设置为表单。
指令示例:
angular
.module('app')
.directive('sameAs', sameAs);
function sameAs() {
var directive = {
link: link,
restrict: 'A',
require: 'ngModel',
scope: {
sameAs: '='
}
};
return directive;
////////////
function link(scope, element, attrs, ctrl) {
ctrl.$validators.match = function (modelValue, viewValue) {
return viewValue === scope.sameAs;
};
}
}
使用示例:
<form name="testForm">
<input name="newPassword" type="password"
ng-model="formData.newPassword">
<input name="confirmPassword" type="password"
ng-model="formData.confirmPassword"
same-as="formData.newPassword">
<span ng-show="testForm.confirmPassword.$error.match">Passwords is not the same!</span>
</form>
于 2015-10-05T13:44:06.953 回答