Reputation: 2355
Why is angular injection syntax is like this :
angular.module('myApp.services', [])
.factory('someService', ['$http', function($http) {
...
}]);
and is not like this for example:
angular.module('myApp.services', [])
.factory('someService', function($http) {
...
});
It look like the interpreter will have the same information in both cases. but with less complexity in the second.
Upvotes: 2
Views: 63
Reputation: 4578
Whenever javascript
code is obfuscated
or minified
then all variable passed in a method or declared in methods gets changed in unknown letter. but angular
need to find them with the actual name like $http
so we don't need to change the exact name of module rather create a copy of different name using angular short notation syntax.
angular.module('myApp.services', [])
.factory('someService', function($http) {
var item,name;
});
After obfuscation.
angular.module('myApp.services', [])
.factory('someService', function(a) {
var b,c;
});
But when code is written in angular short notation it will be like.
angular.module('myApp.services', [])
.factory('someService', ['$http',function(a) {
var b,c;
});
Here '$http' will not be changed because it is a string value and it does not covert at the time of obfuscation.
Now angular knows that variable 'a' passed in the function is $http service.
Upvotes: 1
Reputation: 2423
The first way helpfull when you minimized your current files. Because injector finds modules by name, you need to keep modules name not minimized. So the second way you can use when you don't minimize your files.
Upvotes: 2