Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 1
    Not sure what's the best practice for OP's scenario, but this answer seems so much simpler than the leading ones. Will moving the injector.get() call to the module work (assuming stateless service so several classes can "share" the same instance)? Commented Jun 28, 2020 at 18:44
  • I think that code would end up by all the poney instances sharing the same instance of poney service. What do you think? Commented Jun 30, 2020 at 7:04
  • Julien - that's an acceptable result (actually the preferred one) for my scenario - think of something like an input validator, user permissions handler or localization service - where you need the same functionality across the app but there's no need for a unique instance for each individual consumer of the service. Commented Jul 5, 2020 at 10:47
  • Having some trouble trying to get this to work in Jasmine tests. How to configure the test setup? ServiceLocator.injector keeps returning null, although I have injected the "PoneyService" into the TestBed? Commented Nov 19, 2020 at 18:15
  • 1
    @Julien can you add some explanation to why this answer works? Commented Sep 21, 2021 at 15:38