查看此代码@ plnkr.co。在link
的{{1}}函数中,directive
被调用,效果很好。问题是,当我覆盖controller.$render()
函数时,它不会运行。 controller.$render
未出现在控制台中。
console.log('overridden $render function called');
是:
script.js
var app = angular.module('myApp', []);
app.directive('test', function () {
return {
require: '?ngModel',
link: function ($scope, $element, $attr, controller) {
if (!controller) {
console.log("controller of ngModel not found");
return;
} else {
console.log("controller of ngModel found");
controller.$setViewValue('qwerty');
//controller.$render();
controller.$render = function(){
console.log('overridden $render function called');
}
}
}
};
});
是:
index.html
答案 0 :(得分:1)
覆盖后立即尝试拨打controller.$render()
:
var app = angular.module('myApp', []);
app.directive('test', function () {
return {
require: '?ngModel',
link: function ($scope, $element, $attr, controller) {
if (!controller) {
console.log("controller of ngModel not found");
return;
} else {
console.log("controller of ngModel found");
controller.$setViewValue('qwerty');
//controller.$render();
controller.$render = function(){
console.log('overridden $render function called');
}
controller.$render();
}
}
};
});