質問

var app = angular.module('myApp', []); app.controller('myController', ['$scope', function($scope, []) { ..... }]);

and

var app = angular.module('myApp', []); app.controller('myController', function($scope, []) { ..... });

In first example, square brackets are used around the callback function, but in second example, there are no square brackets around the callback function.

役に立ちましたか?

解決

The second one is used for protection against minification mangling, because when you mangle the first version, you get let's say

var b = a.module('myApp', []);
b.controller('myController', function(a, []) {
      .....
      // if you use $scope here it will get mangled and may not be what you expect, this potentially breaks the code
});

Whereas if you use the second one you protect your variables so that angular knows how you assign injected dependencies and your code does not break anymore woohoo!

他のヒント

You can inject dependencies with a string and variable name, this is used for minification since variable/parameters names are being evaluated and injected.

The first example should actually be written like so:

var app = angular.module('myApp', []);
app.controller('myController',[ '$scope', function($scope, []) {
      .....
]});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top