2

Is it possible to inject services into a route/state resolve when using AngularAMD/RequireJS?

The pattern of using route/state resolves to prepare data for a controller before the controller loads is a nice practice. I find it's especially useful for fetching data from the server and storing that data in a service, and this is something I've done successfully when not using AngularAMD.

When I implement AngularAMD and try to inject a service into the resolve, I get an "Unknown Provider" error, referencing the service that I'm trying to inject.

Here is a basic example that results in an Unknown Provider error being thrown for the OtherService.

$stateProvider
  .state('home', angularAMD.route({
    url: '/home',
    templateUrl: 'home.html',
    controllerUrl: 'HomeCtrl.js',
    controller: 'HomeCtrl',
    resolve: {
      stuff: function(OtherService) {
        return OtherService.getStuff();
      }
    }
  }));

Has anyone encountered this issue? Any solutions and/or explanations for why it doesn't work?

This question - angularjs + requirejs + dependency in resolve - regards the same issue, but the lone answer wasn't particularly helpful.

Community
  • 1
  • 1
zgotts
  • 31
  • 2

0 Answers0