0

I have an extremely edge case scenario where I have a callback method I have to define during config. Which means no scope, no factories, etc... My work around is to use the root injector ($injector) and get my other modules at runtime.

However, when I call $injector.get('myServiceName') in my call back (after the application is running) I get "unknown provider". The same service has no problem (and actually is) being injected into a before my line of code is running. If I call $injector.get("myServiceNameProvider") then I can get the provider back in my callback.. But another service that only has a factory I can't get at all.

So in this extremely bad practice, how can I snag the service I configured. Heck I can't even seem to get $rootScope..

Josh Handel
  • 1,690
  • 2
  • 13
  • 21
  • just tried to invoke with the same result :-( $injector.invoke(['myServiceName', function (myServiceVar) {}]); Dagnabit.. – Josh Handel Apr 14 '17 at 13:47

2 Answers2

0

Angular inits providers first, then factories/services - services are not available in app.config.

You can deo like this:

app.config(...
  window.on('scroll', function() { 
    // this is executed later
    $injector.get()...
  })

Or use app.run where services are available.

Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38
  • yah thats eactly what I was doing, the services still aren't findable... And I don't have a choose as to moving my call to app.run... I have to pass the callback as a configuration for ANOTHER service in its .config... There is an architectural flaw in a Angular service that takes a callback in a config, but thats what I have to work with :-(... \ – Josh Handel Apr 14 '17 at 16:08
0

I think I had similar problem few months ago... I need to construct breadcrumbs, but they had to be created in config phase (ui-router). I have done it like this (breadcrumbs-generation-service.provider.js):

 var somethingYouHaveToHaveLater = undefined;
 function BreadcrumbsGenerationService () {
    this.createStateData = createStateData;

    function createStateData (arg) {
        somethingYouHaveToHaveLater = arg;
    };
}

function BreadcrumbsGenerationServiceProvider () {
    this.$get = function BreadcrumbsGenerationServiceFactory () {
       return new BreadcrumbsGenerationService();
    }
}

angular
    .module('ncAdminApp')
    .provider('BreadcrumbsGenerationService', BreadcrumbsGenerationServiceProvider);

Because service is used inside Angular configs, needs to be injected as provider to be available in config phase: Similar SO Question. Despite the fact is registered as BreadcrumbsGenerationService needs to be injected as BreadcrumbsGenerationServiceProvider to config phase and used with $get():

BreadcrumbsGenerationServiceProvider.$get().createStateData(someParams);

But in controller, inject it without Provider suffix (BreadcrumbsGenerationServic) and it behaves as normal service.

Community
  • 1
  • 1
Radek Anuszewski
  • 1,812
  • 8
  • 34
  • 62
  • but you would think if I call $injector.get('myService') in my call back (which is well after config is done) it would be able to resolve the service.. – Josh Handel Apr 14 '17 at 20:15
  • @JoshHandel Yes, cause injecting is the same like getting from injector - but just give my answer a try, and you will find out :) – Radek Anuszewski Apr 14 '17 at 21:04
  • So I think our problems are sadly a touch different.. I am working with adal, which takes a config during the config part of the bootstrap.. inside adal it copies the config (not by reference :-(... ) so the callback I hand it I can't assign later.... IN that callback to do what I need to do I need $scope (really I need a few other services).. If I could assign something later.. I have also come to learn through experimentation that the $injector in app.config is not the same $injector passed into a controller... So angular manages multiple $injectors :-| – Josh Handel Apr 14 '17 at 21:50
  • I just had an idea to modify the angular module (adal-angular) so that the callback I am trying to work with (displayCall) can be set during app.run instead of this chicken/egg issue.. I hate having to deviate from a package that I don't own, but I guess if it works I'll submit a pull request.. silly Adal guys.. – Josh Handel Apr 14 '17 at 21:50
  • @JoshHandel Could toy provide a Plunker with your problem? It will help to understand this weird behavior. – Radek Anuszewski Apr 15 '17 at 09:18