简体   繁体   English

如何将依赖项注入命名的角度指令控制器?

[英]How to inject dependencies to a named angular directive controller?

If I have code similar to this question on injecting another controller to a directive : 如果我有与此问题类似的代码,将另一个控制器注入指令

angular.module('paramApp', []);

angular.module('paramApp').controller('ParamCtrl', ['$scope', '$http', function($scope, $http) {
   .
   .
   .
}]);

angular.module('deviceApp', ['paramApp']);
angular.module('deviceApp').directive('DeviceDirective', function () {    
    return { 
    .
    .
    .
    controller: 'ParamCtrl' 
    };
});

When I minify js, the injected dependencies of $scope and $http break, how can I explicitly define the dependencies of ParamCtrl when creating DeviceDirective to prevent uncaught injector issues with bundled js ? 当我缩小js时,注入的$scope$http依赖项会中断,如何在创建DeviceDirective时显式定义ParamCtrl的依赖项,以防止绑定的js出现未捕获的注入器问题

I am very late to this question but I'll give it a shot. 我对这个问题很晚了,但我会试一试。 The syntax is based on John Papa's Angular style guide 语法基于John Papa的Angular样式指南

First you need a way to make your controller reusable. 首先,您需要一种使控制器可重用的方法。 Convert it from an anonymous function to a named function and pass it to your angular app like so: 将其从匿名函数转换为命名函数,然后将其传递给您的angular应用程序,如下所示:

// Named controller function
ParamCtrl function($scope, $http) {
   this.doStuff
}

// Bind it to your app
angular.module('paramApp').controller('ParamCtrl', ['$scope', '$http', ParamCtrl );

// While we are at it, do the same with your directive
DeviceDirective function (controlerParam) {    
    return { 
        ...
        controller: controlerParam
    } 
}

// Bind it to your app
angular.module('deviceApp', ['ParamCtrl', DeviceDirective]);

However, if you meant to pass the controller's scope to your directive refer to fiznool's post 但是,如果您打算将控制器的范围传递给指令,请参阅fiznool的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM