Ali Ameer
Ali Ameer

Reputation: 49

Difference between dependency injection syntaxes in AngularJS

AngularJS supports two slightly different syntaxes for dependency injection

Syntax 1

myModule.controller('myCtrl', function($scope, $http, myService) {
    ...
    ...
});

Syntax 2

myModule.controller('myCtrl', ['$scope', '$http', 'myService', function($scope, $http, myService) {
    ...
    ...
}]);


Is there a fundamental difference between the two syntaxes?
When to use either of the two syntaxes?

Upvotes: 0

Views: 35

Answers (1)

taxicala
taxicala

Reputation: 21789

Syntax 2 is called type hinting, if you plan to uglify and mangle your code, angular would still know what services to inject.

After mangling and ugilying:

myModule.controller('myCtrl', ['$scope', '$http', 'myService', function(a, b, c) {

Angular would read the string values of the array provided in order to determine the name of the services a, b, c in order to inject them properly.

Upvotes: 1

Related Questions