【问题标题】:AngularJS controller syntax - difference between array and function versionAngularJS控制器语法 - 数组和函数版本之间的区别
【发布时间】:2014-10-07 18:19:13
【问题描述】:

我是 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){

});

为什么在第一个版本中有奇怪的语法?

【问题讨论】:

  • 没有功能差异。使用 [] 是为了允许正确读取缩小版本。

标签: javascript angularjs angularjs-controller


【解决方案1】:

在缩小 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){

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2016-11-20
    • 2010-09-30
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多