user2924127
user2924127

Reputation: 6242

controller dependencies in angularjs

I am new to angular and when I read tutorials I see two different methods of declaring dependencies in controllers:

1)

angular.module("myApp",[]).controller('MyController, function($scope, $localStorage){
});

And others have a little different way:

2)

angular.module("myApp",[]).controller('MyController, ['$scope', '$localStorage', function($scope,$localStorage){
}]);

The second way seems to be redundant to me since I have to specify $scope and $localStorage twice? What is the difference between these two ways of defining a controller?

Upvotes: 3

Views: 146

Answers (1)

Kevin F
Kevin F

Reputation: 2885

The second way is minification friendly. When your code is minified

angular.module("myApp",[]).controller('MyController, function($scope, $localStorage){
});

will turn into something like

angular.module("myApp",[]).controller('MyController, function(a,b){
});

The second way keeps a reference to the object you are passing in. You can check the docs here, scroll down to "a note on minification"

Upvotes: 5

Related Questions