问题是只有第一个指令正在运行而另一个指令不正常。以下是两个指令。单独工作时两者都可以正常工作。
.directive("restrictInvalidInput", function($rootScope, utils) {
return {
restrict: "A",
require: "ngModel",
priority: 1,
link: function(scope, elem, attr, ctrl) {
var remove_invalid_chars = function() {
//blah blah
};
ctrl.$parsers.push(remove_invalid_chars);
}
}
})
.directive("validateUsername", function($rootScope, utils) {
return {
restrict: "A",
require: "ngModel",
priority: 2,
link: function(scope, elem, attr, ctrl) {
var validate_username = function(modelValue, viewValue) {
//blah blah
};
ctrl.$validators.valid_username = validate_username;
}
}
});
答案 0 :(得分:1)
似乎问题出在[1, 2, 3]
的位置。您确定,该解析器返回一个值吗?因为(from docs):
从解析器返回
blah blah
表示发生了解析错误。在这种情况下,不会运行undefined
$validators

angular.module('app', [])
.controller('Controller', ['$scope', function($scope) {
$scope.name = 'Some name';
}])
.directive("restrictInvalidInput", function() {
return {
restrict: "A",
require: "ngModel",
priority: 1,
link: function(scope, elem, attr, ctrl) {
var remove_invalid_chars = function(viewValue) {
console.log('Look, I\'m parsing');
return viewValue;
};
ctrl.$parsers.push(remove_invalid_chars);
}
}
})
.directive("validateUsername", function() {
return {
restrict: "A",
require: "ngModel",
priority: 2,
link: function(scope, elem, attr, ctrl) {
var validate_username = function(modelValue, viewValue) {
console.log('Look, I\'m validating');
return true;
};
ctrl.$validators.valid_username = validate_username;
}
}
});