<input type='password' id='pw1' name='pw1'
ng-model='pw1' ng-required/>
<input type='password' id='pw2' name='pw2'
ng-model='pw2' ng-required pw-check='pw1'/>
指令定义部分:
angular.module('myApp.directives', [])
.directive('pwCheck', [function () {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
var firstPassword = '#' + attrs.pwCheck;
elem.add(firstPassword).on('keyup', function () {
scope.$apply(function () {
var v = elem.val()===$(firstPassword).val();
ctrl.$setValidity('pwmatch', v);
});
});
}
}
}]);
错误提示<div class='msg-block' ng-show='myForm.$error'>
<span class='msg-error' ng-show='myForm.pw2.$error.pwmatch'>
Passwords don't match.
</span>
</div>