Question

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.

Était-ce utile?

La solution

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!

Autres conseils

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, []) {
      .....
]});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top