Shyamal Parikh
Shyamal Parikh

Reputation: 3068

Angularjs: Inject Service in app.config such that to safeguard against minification

I am trying to inject a service in app.config as illustrated in Inject service in app.config. However, minification breaks the app. How to overcome this?

This doesn't work with minification:

app.config(['$routeProvider',function ($routeProvider) {
    $routeProvider
        .when('/',
        {
            templateUrl: "partials/editor.html",
            controller: "AppCtrl",
            resolve: {
                       //Following method doesn't work with minification
                        data: getData
                     }
        })
     function getData (dbService) {
         return dbService.getData();
     }
}]);

Please note the following code doesn't work: (Typescript does not allow compilation)

['dbService',function getData(dbService){
               return dbService.getData();
}]

Upvotes: 2

Views: 101

Answers (1)

Dustin Hodges
Dustin Hodges

Reputation: 4195

In order to safeguard against minification, you need to annotate (see Dependency Annotation here) the data function like you did with the config function.

There are two ways to do this.

1.

Instead of passing a function, pass an array with the names of the dependencies and the function

app.config(['$routeProvider',function ($routeProvider) {
$routeProvider
    .when('/',
    {
        templateUrl: "partials/editor.html",
        controller: "AppCtrl",
        resolve: {
            //annotate this to prevent against minification
            data: ['dbService', getData]
        }
    })

    function getData (dbService) {
        return dbService.getData();
    }
}]);

2.

Add your dependencies to a $inject property on your function

app.config(['$routeProvider',function ($routeProvider) {
    $routeProvider
        .when('/',
        {
            templateUrl: "partials/editor.html",
            controller: "AppCtrl",
            resolve: {
                //function annotated below
                data: getData
            }
        })

    //annotate this function with $inject so proper dependencies injected after minification
    getData.$inject = ['dbService'];
    function getData (dbService) {
        return dbService.getData();
    }
}]);

Upvotes: 4

Related Questions