0

what's the difference of defining controller's dependencies in array:

app.controller('IndexController', ['$rootScope', '$http', function($rootScope, $http) {
    //some cool stuff
}]);

and injecting them right into the function like this:

app.controller('IndexController', function($rootScope, $http) {
    //some cool stuff
});

Lot of posts and tutorials use the shorter version, so I'm wondering if there's any advantage of doing it the first way.

Thanks!

1
  • The problem will be created during minification of code. Since the string are not mutated during minification, so the first one will work well after minification where as second one will create problem. Commented Jan 8, 2015 at 16:38

1 Answer 1

2

This is necessary if you use some minification tools such as uglify. These kind of tools change the name of the variables, thus, for example:

app.controller('IndexController', function($rootScope, $http) {
    //some cool stuff
});

Becomes something like:

randomVariable.controller('IndexController',function(a, b){});

And a and b are not your dependencies.

In the other case, the minified code becomes something like:

app.controller('IndexController',['$rootScope','$http',function(a,b)

Here a and b are passed as arguments from two strings that are values and hence they cannot be modified by the minification tools

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

2 Comments

Thanks! So there's no need to use the longer way if you're not going to minify the script, right?
s. there is no need to use that approach unless you minify

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.