我试图建立这验证使用来自外部范围通过自定义函数输入字段指令,例如:角自定义的验证指令断裂其它指令
HTML:
<input type="text" custom-validator="checkValidity"/>
控制器:
$scope.checkValidity = function(value){
return $scope.invalidWords.indexOf(value) === -1;
}
我创建了一个详细的plunker此:http://plnkr.co/edit/H5A5O3?p=preview
验证工作,但它似乎打破了默认指令,在这种情况下ng禁用不起作用,我不能访问ng模型中使用的变量!
这是我的指令:
app.directive('customValidator', function() {
return {
require: "ngModel"
, scope: { customValidator: '='}
, link: function postLink(scope, elm, attrs, ctrl) {
var validator = function(value) {
if(scope.customValidator && scope.customValidator(value)) {
ctrl.$setValidity('custom-validator', true);
return value;
}
ctrl.$setValidity('custom-validator', false);
return undefined;
}
ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
}
});
我想不出什么错误,我真的需要帮助!
我应该留在角1.0.7
它会工作_but_我无法传递一个自定义函数。在我的情况下,我必须使用3种不同的函数来输入3个表单。我试过'transclude:true',但它似乎不能解决这个问题... – destegabry
@destegabry看看我使用'attrs'的更新指令。 – AlwaysALearner
非常感谢你! – destegabry