admin 管理员组文章数量: 1086019
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
1 Answer
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).
本文标签: javascriptAngularJS parsers vs validatorsStack Overflow
版权声明:本文标题:javascript - AngularJS: $parsers vs $validators - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744069442a2528218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论