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

How can I access the controller instance associated with a directive from the link function?

本文关键字:关联 控制器 实例 指令 link 函数 访问      更新时间:2023-09-26

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

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) {}
};

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

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