AngularJS - 是否有任何依赖注入容器?

时间:2015-05-23 12:57:37

标签: angularjs

查看我的AngularJS控制器:

MyApp.controller('MyAppController', function($scope, $http, $filter, 
                                             $location, $window, FileUploader) {
    // ...
});

是否有任何依赖注入容器或当前方法看起来不错?

2 个答案:

答案 0 :(得分:3)

Angularjs会为您处理依赖注入,但是要将依赖项放在如下所示的数组中,请明确说明应该注入的内容。如果函数中的参数被缩小为' a'或' b',则数组让angularjs知道要注入的内容。

MyApp.controller('MyAppController', ['$scope', '$http', '$filter', '$location', '$window', 'FileUploader', function($scope, $http, $filter, $location, $window, FileUploader) {

 ...

}])

答案 1 :(得分:3)

它看起来很好,但是如果您打算稍后缩小代码,则需要编写这样的代码,以便Angular知道要注入的内容:

MyApp.controller('MyAppController', ['$scope', '$http', '$filter', '$location', '$window', 'FileUploader', function($scope, $http, $filter, $location, $window, FileUploader) {

 ...

}]);

如果您使用Gulp,我会发现一个插件ng-annotate非常有用,它会为您处理这些额外的代码。使您不必再写两次。

相关问题