我是解析器和格式化程序的新手。我有一个指令,将对模型的变化进行验证。要做到这一点的一种方法是$ watch,但从我所了解的情况来看,这不是一种好方法,因为它允许更新模型。在输入文本框中不会调用解析器函数
所以我一直在寻找的解析器,并试图将此代码
app.directive('myDirective', function($compile) {
return {
restrict: 'E',
require: 'ngModel',
scope: {
},
link: function($scope, elem, attr, ctrl) {
console.debug($scope);
ctrl.$formatters.push(function(value) {
console.log("hello1");
return value;
});
ctrl.$parsers.unshift(function(value) {
debugger;
console.log("hello");
return value;
});
}
};
});
但解析器函数永远不会被调用。格式化程序被调用一次。 Please see the plunkr。任何人都可以告诉我我在做什么错,为什么在我输入文本框时解析器函数没有被调用?
尝试ctrl。$ parsers.push,仍然不起作用 – Abhik