如何解决角度LocalStorageModule在ngmin之后使用$ injector:modulerr消息失败的问题

时间:2014-05-06 15:39:11

标签: angularjs gruntjs yeoman mean-stack

我浏览了yeoman tutorial,当我执行grunt serve时,生成的应用程序运行正常。但是,当我执行grunt serve:dist时失败。错误消息是:

Uncaught Error: [$injector:modulerr] Failed to instantiate module mytodoApp due to: Error: [$injector:unpr] Unknown provider: a

在我的app.js文件中,如果我注释掉configure的调用(设置前缀),那么问题就会消失:

angular.module('mytodoApp', ['LocalStorageModule', 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute', 'ui.sortable'])
.config(['localStorageServiceProvider', function (localStorageServiceProvider) {
    localStorageServiceProvider.setPrefix('ls');
}])
.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'views/main.html',
            controller: 'MainCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
});

3 个答案:

答案 0 :(得分:4)

我有同样的问题。正如Eddie所建议的那样,我最终将两个.config部分组合在一起:

.config(function ($routeProvider, localStorageServiceProvider) {
    localStorageServiceProvider.setPrefix('ls');

    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

答案 1 :(得分:3)

按如下方式包装我的app.js文件解决了问题:

(function() {
    var app = angular.module('mytodoApp', ['LocalStorageModule','ngCookies','ngResource','ngSanitize','ngRoute','ui.sortable']);
    app.config(['localStorageServiceProvider', function (localStorageServiceProvider) {
        localStorageServiceProvider.setPrefix('ls');
    }]);
    app.config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });
    });
}.call(this));

答案 2 :(得分:1)

您需要在ngMin安全代码中包装第二个.config()块:

angular.module('mytodoApp', ['LocalStorageModule', 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute', 'ui.sortable'])
.config(['localStorageServiceProvider', function (localStorageServiceProvider) {
    localStorageServiceProvider.setPrefix('ls');
}])
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'views/main.html',
            controller: 'MainCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
}]);

或者组合两个街区。