How to resolve issue with angular LocalStorageModule failing with $injector:modulerr message after ngmin

StackOverflow https://stackoverflow.com/questions/23499098

Question

I went through the yeoman tutorial and when I execute grunt serve the resulting app works fine. However it fails when I execute grunt serve:dist. The error message is:

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

In my app.js file if I comment out the call to configure (which sets the prefix) then the issue goes away:

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: '/'
        });
});
Was it helpful?

Solution 2

wrapping my app.js file as follows resolved the issue:

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

OTHER TIPS

I had the same issue. As suggested by Eddie, I ended up combining the two .config sections like so:

.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: '/'
      });
  });

You need to wrap the second .config() block in ngMin safe code:

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: '/'
        });
}]);

Or combine the two blocks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top