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