Soviut
Soviut

Reputation: 91555

How to inject dependencies in app.config in angularjs?

I've been getting errors when minifying my AngularJS app because manually injected dependencies aren't working how I'd expect. The following didn't work:

var config = app.config(function($routeProvider) {
    $routeProvider
        .when('/', {controller: 'PageCtrl', templateUrl: '../templates/home.html'});
        .otherwise({redirectTo: '/'});
});
config.$inject = ['$routeProvider'];

The only thing that worked is:

app.config(['$routeProvider', function($routeProvider) {
    ...
}]);

Why does the first dependency injection technique work for controllers but not configs?

Upvotes: 1

Views: 2805

Answers (1)

ardentum-c
ardentum-c

Reputation: 1410

It is because app.config returns reference to the app (for chaining). This code works:

var config = function($routeProvider) {
    $routeProvider
        .when('/', {controller: 'PageCtrl', templateUrl: '../templates/home.html'})
        .otherwise({redirectTo: '/'});
};

config.$inject = ['$routeProvider'];
app.config(config);

http://jsfiddle.net/ADukg/3196/

Upvotes: 2

Related Questions