3

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?

1 Answer 1

5

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"

Sign up to request clarification or add additional context in comments.

1 Comment

If you want to have more details on what Kevin is talking about, see docs.angularjs.org/guide/di (Dependency Annotation). You have an explanation on the three ways of declaring your components with the pros and cons ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.