gpt4 book ai didi

angularjs - $parser.unshift??这是如何工作的?

转载 作者:行者123 更新时间:2023-12-03 01:09:27 27 4
gpt4 key购买 nike

 link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {

scope.pwdValidLength = (viewValue && viewValue.length >= 8 ? 'valid' : undefined);
scope.pwdHasLetter = (viewValue && /[A-z]/.test(viewValue)) ? 'valid' : undefined;
scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? 'valid' : undefined;

if(scope.pwdValidLength && scope.pwdHasLetter && scope.pwdHasNumber) {
ctrl.$setValidity('pwd', true);
return viewValue;
} else {
ctrl.$setValidity('pwd', false);
return undefined;
}

});
}

http://jsfiddle.net/adamdbradley/Qdk5M/

在上面提到的 fiddle 中,密码验证是如何进行的?$parser.unshift 是做什么的? test(viewValue) 有什么用......?我引用了 AngularJs 主站点,但什么也看不懂......请逐步指导我如何验证...

我是 angularJS 新手..

最佳答案

下面是分步说明。请注意,文档非常好:the forms 上的页面以及 the $parsers就是您要找的人。

link: function(scope, elm, attrs, ctrl) {
/**
* This function is added to the list of the $parsers.
* It will be executed the DOM (the view value) change.
* Array.unshift() put it in the beginning of the list, so
* it will be executed before all the other
*/
ctrl.$parsers.unshift(function(viewValue) {

scope.pwdValidLength = (viewValue && viewValue.length >= 8 ? 'valid' : undefined); // Check the length of the string
scope.pwdHasLetter = (viewValue && /[A-z]/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains letter. RegExp.test() simply returns a boolean if the string matches the regex.
scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains digit. Same remark.

if(scope.pwdValidLength && scope.pwdHasLetter && scope.pwdHasNumber) { // If all is good, then…
ctrl.$setValidity('pwd', true); // Tell the controlller that the value is valid
return viewValue; // Return this value (it will be put into the model)
} else { // … otherwise…
ctrl.$setValidity('pwd', false); // Tell the controlller that the value is invalid
return undefined; // When the value is invalid, we should return `undefined`, as asked by the documentation
}

});
}

关于angularjs - $parser.unshift??这是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19091685/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com