mfc
mfc

Reputation: 3026

calling angularjs config block

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?

Upvotes: 0

Views: 90

Answers (2)

Shekhar Khairnar
Shekhar Khairnar

Reputation: 2691

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.

Upvotes: 1

Pierre-Alexandre Moller
Pierre-Alexandre Moller

Reputation: 2354

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.

Upvotes: 1

Related Questions