AngularJS控制器语法 - 数组和函数版本之间的区别

9
我是一名新手学习AngularJS。那么用声明数组参数的控制器和将依赖项列举为字符串和JavaScript名称的控制器有什么区别呢?请注意,以下内容仅供参考:

app.controller("firstController", ['$scope', '$modal', '$log', 'HttpService', 'FisrtSharedService', 'SecondSharedService', function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService) {

}]);

...以及这个表单,只列出JavaScript名称?

app.controller("firstController", function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService){

});

为什么第一个版本的语法看起来很奇怪?

4
没有功能上的区别。使用 [] 的目的是为了让压缩版本能够正确读取。 - Zack Argyle
1个回答

12

当你压缩JS文件时,'$scope', '$modal', '$log', 'HttpService', 'FisrtSharedService', 'SecondSharedService'只是声明了注入器。

app.controller("firstController", ['$scope', '$modal', '$log', 'HttpService',    'FisrtSharedService', 'SecondSharedService', function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService) {

}]);
你也可以像这样声明注入器:
firstController.$inject = ['$scope', '$modal', '$log', 'HttpService',    'FisrtSharedService', 'SecondSharedService'];
app.controller("firstController", function($scope, $modal, $log, HttpService,  FisrtSharedService, SecondSharedService){

});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接