4 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
這應(yīng)該解決它:
視圖:
<div ng-controller='Ctrl'>
<form name='form'>
<input data-ng-model='user.password' type="password" name='password' placeholder='password' required>
<div ng-show="form.password.$error.required">
Field required</div>
<input ng-model='user.password_verify' type="password" name='confirm_password' placeholder='confirm password' required data-password-verify="user.password">
<div ng-show="form.confirm_password.$error.required">
Field required!</div>
<div ng-show="form.confirm_password.$error.passwordVerify">
Fields are not equal!</div>
</form
</div>
指示
var app = angular.module('myApp', []);
app.directive("passwordVerify", function() {
return {
require: "ngModel",
scope: {
passwordVerify: '='
},
link: function(scope, element, attrs, ctrl) {
scope.$watch(function() {
var combined;
if (scope.passwordVerify || ctrl.$viewValue) {
combined = scope.passwordVerify + '_' + ctrl.$viewValue;
}
return combined;
}, function(value) {
if (value) {
ctrl.$parsers.unshift(function(viewValue) {
var origin = scope.passwordVerify;
if (origin !== viewValue) {
ctrl.$setValidity("passwordVerify", false);
return undefined;
} else {
ctrl.$setValidity("passwordVerify", true);
return viewValue;
}
});
}
});
}
};
});

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
我使用以下指令,因?yàn)槲蚁胫匦买?yàn)證兩個(gè)輸入字段,而不管值1或值2是否更改:
指示:
'use strict';
angular.module('myApp').directive('equals', function() {
return {
restrict: 'A', // only activate on element attribute
require: '?ngModel', // get a hold of NgModelController
link: function(scope, elem, attrs, ngModel) {
if(!ngModel) return; // do nothing if no ng-model
// watch own value and re-validate on change
scope.$watch(attrs.ngModel, function() {
validate();
});
// observe the other value and re-validate on change
attrs.$observe('equals', function (val) {
validate();
});
var validate = function() {
// values
var val1 = ngModel.$viewValue;
var val2 = attrs.equals;
// set validity
ngModel.$setValidity('equals', ! val1 || ! val2 || val1 === val2);
};
}
}
});
用法
<input type="password" ng-model="value1" equals="{{value2}}" required>
<input type="password" ng-model="value2" equals="{{value1}}" required>

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
不需要為此創(chuàng)建單獨(dú)的指令。Angular UI密碼驗(yàn)證工具中已經(jīng)存在一個(gè)內(nèi)部版本。這樣,您可以執(zhí)行以下操作:
<input name="password" required ng-model="password">
<input name="confirm_password"
ui-validate=" '$value==password' "
ui-validate-watch=" 'password' ">
Passwords match? {{!!form.confirm_password.$error.validator}}

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
對(duì)此的另一種做法是使一個(gè)輸入的模型與另一個(gè)輸入的值匹配。
app.directive('nxEqual', function() {
? ? return {
? ? ? ? require: 'ngModel',
? ? ? ? link: function (scope, elem, attrs, model) {
? ? ? ? ? ? if (!attrs.nxEqual) {
? ? ? ? ? ? ? ? console.error('nxEqual expects a model as an argument!');
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? scope.$watch(attrs.nxEqual, function (value) {
? ? ? ? ? ? ? ? model.$setValidity('nxEqual', value === model.$viewValue);
? ? ? ? ? ? });
? ? ? ? ? ? model.$parsers.push(function (value) {
? ? ? ? ? ? ? ? var isValid = value === scope.$eval(attrs.nxEqual);
? ? ? ? ? ? ? ? model.$setValidity('nxEqual', isValid);
? ? ? ? ? ? ? ? return isValid ? value : undefined;
? ? ? ? ? ? });
? ? ? ? }
? ? };
});
因此,如果密碼盒的型號(hào)為,login.password則在驗(yàn)證盒上設(shè)置以下屬性:nx-equal="login.password",并測(cè)試formName.elemName.$error.nxEqual。像這樣:
<form name="form">
? ? <input type="password" ng-model="login.password">
? ? <input type="password" ng-model="login.verify" nx-equal="login.password" name="verify">
? ? <span ng-show="form.verify.$error.nxEqual">Must be equal!</span>
</form>
擴(kuò)大的視野:
對(duì)于我的一個(gè)新項(xiàng)目,我必須修改上述指令,以使其僅nxEqual在驗(yàn)證輸入具有值時(shí)才顯示錯(cuò)誤。否則nxEqual錯(cuò)誤應(yīng)被忽略。這是擴(kuò)展版本:
app.directive('nxEqualEx', function() {
? ? return {
? ? ? ? require: 'ngModel',
? ? ? ? link: function (scope, elem, attrs, model) {
? ? ? ? ? ? if (!attrs.nxEqualEx) {
? ? ? ? ? ? ? ? console.error('nxEqualEx expects a model as an argument!');
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? scope.$watch(attrs.nxEqualEx, function (value) {
? ? ? ? ? ? ? ? // Only compare values if the second ctrl has a value.
? ? ? ? ? ? ? ? if (model.$viewValue !== undefined && model.$viewValue !== '') {
? ? ? ? ? ? ? ? ? ? model.$setValidity('nxEqualEx', value === model.$viewValue);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? ? model.$parsers.push(function (value) {
? ? ? ? ? ? ? ? // Mute the nxEqual error if the second ctrl is empty.
? ? ? ? ? ? ? ? if (value === undefined || value === '') {
? ? ? ? ? ? ? ? ? ? model.$setValidity('nxEqualEx', true);
? ? ? ? ? ? ? ? ? ? return value;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? var isValid = value === scope.$eval(attrs.nxEqualEx);
? ? ? ? ? ? ? ? model.$setValidity('nxEqualEx', isValid);
? ? ? ? ? ? ? ? return isValid ? value : undefined;
? ? ? ? ? ? });
? ? ? ? }
? ? };
});
您將像這樣使用它:
<form name="form">
? ? <input type="password" ng-model="login.password">
? ? <input type="password" ng-model="login.verify" nx-equal-ex="login.password" name="verify">
? ? <span ng-show="form.verify.$error.nxEqualEx">Must be equal!</span>
</form>
添加回答
舉報(bào)