0
我有以下指令 (從精通Web應用程序開發採取AngularJS書):
angular.module('unique-email-directive', [])
/**
* A validation directive to ensure that the model contains a unique email address
* @param Users service to provide access to the server's user database
*/
.directive('uniqueEmail', ["Users", function (Users) {
return {
require:'ngModel',
restrict:'A',
link:function (scope, element, attrs, ngModelCtrl) {
var original;
// If the model changes, store this since we assume it is the current value of the user's email
// and we don't want to check the server if the user re-enters their original email
ngModelCtrl.$formatters.unshift(function(modelValue) {
original = modelValue;
return modelValue;
});
// using push() here to run it as the last parser, after we are sure that other validators were run
ngModelCtrl.$parsers.push(function (viewValue) {
if (viewValue && viewValue !== original) {
Users.query({email:viewValue}, function (users) {
if (users.length === 0) {
ngModelCtrl.$setValidity('uniqueEmail', true);
} else {
ngModelCtrl.$setValidity('uniqueEmail', false);
}
});
return viewValue;
}
});
}
};
}]);
我需要澄清有關是否重要將格式化程序功能放置在$ formatters數組的開頭和$ parsers數組的末尾的解析器功能。
如果訂單很重要,請解釋原因。
謝謝drex!您能否提供您引用的節錄鏈接? – balteo
抱歉。它在這裏:http://docs.angularjs.org/api/ng/type/ngModel.NgModelController#$parsers – xyclos