【发布时间】:2017-06-13 20:45:33
【问题描述】:
我试图从控制器中的指令调用验证函数。有可能吗?
我的指令是这样的:
app.directive('evenNumber', function(){
return{
require:'ngModel',
link: function(scope, elem, attrs, ctrl){
ctrl.$parsers.unshift(checkForEven);
function checkForEven(viewValue){
if (parseInt(viewValue)%2 === 0) {
ctrl.$setValidity('evenNumber',true);
}
else{
ctrl.$setValidity('evenNumber', false);
}
return viewValue;
}
}
};
});
我想从控制器调用函数 checkForEven。
$scope.copyFileContentToEditor = function(){
$scope.code = $scope.content;
// TODO call the checkForEven directive function to validate the $scope.code
}
有可能做到吗?有什么建议吗?
【问题讨论】:
-
来自 evenNumber 指令控制器?
-
我想在$scope.copyFileContentToEditor函数中调用'evenNumber'指令的checkForEven函数
标签: angularjs angularjs-directive angular-controller