加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:00:54 所属栏目:安全 来源:网络整理
导读: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' : undefine
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/

在上面提到的小提琴中,密码验证是如何进行的?
$ parser.unshift做了什么?什么是test(viewValue)…..的用途?
我已经推荐了AngularJs的主要网站,但无法理解任何事情……
请一步一步指导我如何验证……

我是angularJS的新手..

以下是逐步说明。请注意,文档非常好: the forms和 the $parsers上的页面是您正在寻找的页面。
link: function(scope,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
        }

    });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读