最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: $parsers vs $validators - Stack Overflow

programmeradmin2浏览0评论

In AngularJS both $parsers and $validators can be used to validate forms. I was wondering what exactly the difference is between using a $parser and using a $validator.

Let's look at the following example:

Validation using a parser

angular.module("myFormApp")
    .directive("containsWhiteSpace", containsWhiteSpace);

function containsWhiteSpace () {

    function hasWhiteSpace(s) {
        return s.indexOf(' ') >= 0;
    }

    return {
        require: "ngModel",
        link: function(scope, ele, attrs, ctrl){

            ctrl.$parsers.unshift(function(value) {
                if(value){
                    var isValid = !hasWhiteSpace(value);
                    ctrl.$setValidity('hasWhiteSpace', isValid);
                }

                return isValid ? value : undefined;
            });

        }
    }
}

Validation using a validator

angular.module("myFormApp")
    .directive("containsWhiteSpace", containsWhiteSpace);

function containsWhiteSpace () {

    function hasWhiteSpace(s) {
        return s.indexOf(' ') >= 0;
    }

    return {
        require: "ngModel",
        link: function(scope, ele, attrs, ctrl){
            ctrl.$validators.hasWhiteSpace = function(value){
                return !hasWhiteSpace(value);
            }
        }
    }
}

They both do the same thing, but when is it correct to use a parser and when is it correct to use a validator? What are the advantages of both? What are the differences?

In AngularJS both $parsers and $validators can be used to validate forms. I was wondering what exactly the difference is between using a $parser and using a $validator.

Let's look at the following example:

Validation using a parser

angular.module("myFormApp")
    .directive("containsWhiteSpace", containsWhiteSpace);

function containsWhiteSpace () {

    function hasWhiteSpace(s) {
        return s.indexOf(' ') >= 0;
    }

    return {
        require: "ngModel",
        link: function(scope, ele, attrs, ctrl){

            ctrl.$parsers.unshift(function(value) {
                if(value){
                    var isValid = !hasWhiteSpace(value);
                    ctrl.$setValidity('hasWhiteSpace', isValid);
                }

                return isValid ? value : undefined;
            });

        }
    }
}

Validation using a validator

angular.module("myFormApp")
    .directive("containsWhiteSpace", containsWhiteSpace);

function containsWhiteSpace () {

    function hasWhiteSpace(s) {
        return s.indexOf(' ') >= 0;
    }

    return {
        require: "ngModel",
        link: function(scope, ele, attrs, ctrl){
            ctrl.$validators.hasWhiteSpace = function(value){
                return !hasWhiteSpace(value);
            }
        }
    }
}

They both do the same thing, but when is it correct to use a parser and when is it correct to use a validator? What are the advantages of both? What are the differences?

Share Improve this question asked Jun 24, 2015 at 14:46 koningdavidkoningdavid 8,1137 gold badges37 silver badges47 bronze badges 1
  • 2 $validators was only introduced in 1.3+ whereas if you use < 1.2 then you use $parsers – gerl Commented Jun 24, 2015 at 15:22
Add a ment  | 

1 Answer 1

Reset to default 10

$parsers are run in a pipeline, to transform the input value. They can return undefined if they fail to parse the input, indicating a parse error. If the input value fails to parse, the validators are not run.

$validators are run after parsers, on the parsed value. They can return false to indicate a data error.

Basically

  • use parsers to coerce a string input value into the data type you need/expect.
  • use validators to validate a value of the correct data type.

For example, consider a model requiring a positive number. $parsers can call parseFloat on the value, and $validators can check if the value is positive.

For the example you gave, I think $validators are more appropriate (and cleaner).

发布评论

评论列表(0)

  1. 暂无评论