如何从链接函数访问与指令关联的控制器实例?

时间:2015-08-28 10:29:55

标签: javascript angularjs

如何从链接函数访问与指令关联的控制器实例?

return {
            template: template,
            controller: controller,
            controllerAs: 'myCtrl', // How do I access the controller instance from the link function?
            restrict: 'E',
            replace: true,
            link: function(scope, element, attrs) {}
};

1 个答案:

答案 0 :(得分:1)

指令中的link函数接受第4个参数。您可以执行以下操作:

         return {
            template: template,
            controller: controller,
            controllerAs: 'myCtrl', 
            restrict: 'E',
            replace: true,
            link: function(scope, element, attrs, controller) {}
         };