Skip to main content
 首页 » 编程设计

javascript之以 Angular 从不同模块访问 Controller

2025年12月25日33TianFang

我正在使用 angular.layout 库 ( https://github.com/angular-ui/ui-layout )。我想从指令中手动切换面板。

它是库中的 Controller :

... 
angular.module('ui.layout', []) 
  .controller('uiLayoutCtrl', ['$scope', '$attrs', '$element', 'LayoutContainer', function uiLayoutCtrl($scope, $attrs, $element, LayoutContainer) 
... 

这个 Controller 有方法 ctrl.toggleAfterctrl.toggleBefore

所以,问题是:如何在我自己的指令中使用它。我试过这种方式:

... 
return { 
        require: 'uiLayoutCtrl', 
        link: function(scope, element, attrs, ctrl) { 
            ... 
        } 
 
    } 
... 

它不起作用。

谢谢!

请您参考如下方法:

尝试要求指令而不是 Controller :

require: 'uiLayout', // both directives in the same element // OR 
//require: '^uiLayout' //  your directive is a child of uiLayout 
link: function (scope, elm, attr, ctrl) { 
    // ctrl should be uiLayoutCtrl 
}