gpt4 book ai didi

javascript - 如何将依赖项注入(inject)到命名的 Angular Directive(指令) Controller ?

转载 作者:行者123 更新时间:2023-12-03 09:47:11 27 4
gpt4 key购买 nike

如果我有类似于 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'
};
});

当我缩小js时,$scope$http的注入(inject)依赖项中断,我如何显式定义ParamCtrl的依赖项创建 DeviceDirective 来防止 uncaught injector issues with bundled js

最佳答案

我很晚才回答这个问题,但我会尝试一下。语法基于 John Papa's Angular style guide

首先,您需要一种使 Controller 可重用的方法。将其从匿名函数转换为命名函数并将其传递给您的 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]);

但是,如果您打算将 Controller 的范围传递给指令,请参阅 fiznool's post

关于javascript - 如何将依赖项注入(inject)到命名的 Angular Directive(指令) Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30985757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com