1

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?

1 Answer 1

2

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/

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

1 Comment

I was so fixated on the lines at the bottom, it took me awhile to realize you'd replaced the app.config with a standard function declaration.

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.