0

When calling an angularjs config block, in some samples I see code like so :-

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    ....
}

and in other samples I see :-

app.config(function($stateProvider, $urlRouterProvider) {
    ....
}

whats the difference?

1

2 Answers 2

1

In first approach first 2 are the alias of providers in function, you can use a particulate provider by it's alias this approach is used in minification process.

app.config(['sateP', 'urlRouterP', function($stateProvider, $urlRouterProvider) {
    ....
   // you can use stateP
}

But in second you cant declare alias for your provider you need to use as it is.

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

1 Comment

The alias isn't the main reason of this. Moreover the alias have to be use in parameter not in the list decleration. Because it's the parameters that are minify and the list of injection which stay. Example here : jsfiddle.net/725htyav with controller.
1

The first one include "annotation" for the minification.

From Angularjs doc (A Note on Minification)

Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

We can overcome this problem by annotating the function with the names of the dependencies, provided as strings, which will not get minified. There are two ways to provide these injection annotations.

So basicaly if you want minify your code, you have to use the 1st syntax.

Comments

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.