1

I'm an Angular beginner, I'm trying to understand the difference between import something in a controller, for example $http, in [] and like parameter in function.

In other words, what is the difference between something like this

.controller('customersCtrl', function($scope, $http) {
    ...
});

and this

.controller('customersCtrl',['$scope', '$http', function($scope, $http) {
   ...
}]);

I checked out documentation and a lot of examples about it but I don't understand.

Thanks in advance and sorry for my basic question.

Regards

1
  • there is no difference between them in terms of functionality except while minification the latter is used for proper injection Commented Feb 12, 2016 at 13:18

2 Answers 2

2

Both do the same thing. But the second method is safer for minification.

If you would minify:

.controller('customersCtrl', function($scope, $http) {
  ...
});

It would break because $scope and $http would be renamed to a and b.

You can read more about it here: https://scotch.io/tutorials/declaring-angularjs-modules-for-minification

As stated in the linked article you can also inject it in the controller using:

mainController.$inject = ['$scope'];
Sign up to request clarification or add additional context in comments.

Comments

0

https://docs.angularjs.org/guide/di - reading the documentation for dependency injection in angular should be enough. More or less it's like what yeouuu said.

When everything is minified and uglified you receive i.e. function(paramOne, paramTwo) -> function(a,b). On runtime you cannot find our which search you actually included. Strings are not minified. So basically on runtime you have mapping between controller('AnyCtrl,['$scope','$http', function(a,b){}]); It knows that a corresponds to be $scope and b corresponds to $http. That's the reason why the order matters as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.