Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I already read the AngularJS documentation but still don't have an answer which I understand.

Why is this used twice? One time as array elements, the second as function parameters.

someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) {
  // ...
}]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

If you minify this code:

someModule.controller('MyController', function($scope, greeter) {
  // ...
});

You'll end with (something like):

someModule.controller('MyController', function(a, b) {
  // ...
});

Angular won't be able to inject the dependencies since the parameters names are lost.

On the other hand, if you minify this code:

someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) {
  // ...
}]);

You'll end with:

someModule.controller('MyController', ['$scope', 'greeter', function(a, b) {
  // ...
}]);

The parameters names are available: Angular's DI is operational.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...