Angularjs:在app.config中注入服务,使其不受缩小的影响

时间:2015-11-25 14:54:39

标签: javascript angularjs

我正在尝试在app.config中注入一个服务,如Inject service in app.config所示。然而,缩小打破了应用程序。如何克服这个?

这不适用于缩小:

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();
                              }
                     }
        })
}]);

3 个答案:

答案 0 :(得分:1)

您需要使用角度文档中所述的内联数组注释来注入它。

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

正如文档中所述,

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

在你的情况下,

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();
                              }]
                     }
        })
}]);

从这里引用:Angularjs: Inject Service in app.config such that to safeguard against minification

答案 1 :(得分:0)

参考angular injection。尝试,

<header>

答案 2 :(得分:0)

为了防止缩小,需要对具有服务依赖性的函数进行数组注释。

不是只传递函数,而是传递一个带有依赖项和函数名称的数组。

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();
    }
}]);