angular - Angular2 - Injecting services by manually resolving the type runtime -


i couldn't figure out right way it, here want achieve:

  • abstract class engine

  • class turboengine extends engine

  • class rocketengine extends engine

i have angular application injecting 'engine' in lot of places , using it's methods fulfill business needs.

my problem have inject turboengine , rocketengine @ same time on every places , condition, decide call.

this want change. since have abstract engine, i'd inject once in main component, , having injected automatically way in components.

so defining provider service @ runtime.

here i've tried, didn't work:

var injector = reflectiveinjector.resolveandcreate([     provide(engine, {useclass: turboengine}) ]); 

i did in main component worked, other places i've got following error:

no provider turboengine! (engine -> turboengine)

updated:

the way want use it:

  • i have component a injects engine (that's want choose implementation, either turboengine or rocketengine based on conditions.

  • then have component b, inside component a's template. here when i'm trying inject engine expect have same instantiated object manually created in component a

reflectiveinjector.resolveandcreate([...]) creates new , independent injector knows engine , entirely unrelated injector used angular application.

you need provide engine bootstrap(...) or @ root component , inject engine need it.


Comments