为什么覆盖`controller。$ render`没有被调用?

时间:2015-10-27 18:28:01

标签: javascript angularjs

查看此代码@ 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

1 个答案:

答案 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();
            }
        }
    };
});
相关问题