0

I read about how to inject dependency in cofing method of the module. I created provider for my service like this

 app.provider("securitySvc", function securitySvcProvider () {
            this.$get = ['Authorizations', '$q', 'routeSvc', function securitySvcFactory (Authorizations, $q, routeSvc) {
                return new securityService(Authorizations, $q, routeSvc);
            }];
        });

When I try to use it I get instance with a $get method instead of newed up securityService. What am I doing wrong?

  app.config( ['$routeProvider', 'routes', 'securitySvcProvider', routeConfigurator]);

 function routeConfigurator($routeProvider, routes, securitySvc) {
   // HERE securitySvc is instance with a $get method, not newed up securitySvc
}

Note that everywhere else, in controllers, securitySvc is injected correctly.

However, if do follwing in "run" method of module

app.run(function ($rootScope, securitySvc) {

        $rootScope.hasPermission = function (authorizationName) {
            return securitySvc.hasAuthorization(authorizationName);
        };
}

Then if I reference it in routeConfiguration through $rootScope.hasPermission it works fine. My goal was to avoid using scope and just use service. Can it be done?

epitka
  • 17,275
  • 20
  • 88
  • 141
  • 2
    What you define in the $get is the factory function. When you inject in the config phase (only providers can be injected) its instance is not available yet. You generally inject it for configuration based on some properties that you expose via the provider(again not its instance). [See the sample and documentation](https://docs.angularjs.org/guide/providers#provider-recipe). – PSL Jan 20 '15 at 21:31
  • @PSL: Ok, so I cannot get instance of the service at this phase. I see now at the bottom of the document it says: "During application bootstrap, before Angular goes off creating all services, it configures and instantiates all providers. We call this the configuration phase of the application life-cycle. During this phase, services aren't accessible because they haven't been created yet." – epitka Jan 20 '15 at 21:39
  • yes that is right... You would need to instantiate them on your own using $injector inside a provider, and while instantating one you would need some other service which this service uses so you need to go deep instantiating all the services that are ultimately needed to instantiate your factory. – PSL Jan 20 '15 at 21:40

1 Answers1

0

Although question was why instance of the provider is injected into config method of the module and PSL explained it in comments, what I really wanted was to find out how to use service in config method. Here is how I achieved what I wanted. I added a method on the provider that provides the check I needed.

 app.provider("securitySvc", function securitySvcProvider() {

        var me = this;

        me.$get = ['Authorizations', '$q', 'routeSvc', function securitySvcFactory(Authorizations, $q, routeSvc) {
            me.service = new securityService(Authorizations, $q, routeSvc);
            return me.service;
        }];

        me.hasRoutePermission = function(routeName) {
            return me.service.hasRoutePermission(routeName);
        }

    });

Since this check is attached to "resolve" object on the router, I can be sure that service will by then be instantiated.

epitka
  • 17,275
  • 20
  • 88
  • 141