Shyamal Parikh
Shyamal Parikh

Reputation: 3068

Angularjs: Inject Service in app.config such that it is not affected by 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: function (dbService) {
                                 return dbService.getData();
                              }
                     }
        })
}]);

Upvotes: 0

Views: 86

Answers (3)

Shyamal Parikh
Shyamal Parikh

Reputation: 3068

In order to safeguard against minification, one needs to array annotate the function with service dependencies.

Instead of passing just the 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();
    }
}]);

Upvotes: 1

Code Apprentice
Code Apprentice

Reputation: 522

You need to inject it using the inline array annotation as stated in the angular docs.

Careful: If you plan to minify your code, your service names will get renamed and break your app.

As stated in the docs,

someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) {
  // ...
}]);

In your case,

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

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

Upvotes: 1

Azad
Azad

Reputation: 5264

refer angular injection. try,

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

Upvotes: 0

Related Questions