sourcecode

$parser.unshift ?이것은 어떻게 됩니까?

copyscript 2023. 10. 26. 21:21
반응형

$parser.unshift ?이것은 어떻게 됩니까?

 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가 처음입니다.

아래는 단계별 설명입니다.설명서가 정말 훌륭합니다. 양식있는 페이지와 $paraser에 있는 페이지가 바로 여러분이 찾고 있는 페이지입니다.

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
        }

    });
}

@Pawan Kalyan - Blackhole은 그것을 아주 멋지게 설명했습니다 (그의 대답에 찬성표를 던졌습니다).하지만 이 유용한 블로그도 읽어보셔야 할 것 같습니다:-
Angular JS의 사용자 정의 검증 지침에서 $pars$formaters -

http://java.dzone.com/articles/parsers-and-formatters-custom

해피코딩 :)

언급URL : https://stackoverflow.com/questions/19091685/parser-unshifthow-does-this-work

반응형