first off, we probably have an unusual angular2 architecture where we bootstrap multiple components (widgets) on a single page and not a single app-component that handles everything.
My first question is, whether the following two implementations are the same, from what I understood, they are (please correct me if I misunderstood something):
bootstrap(SomeComponent, [HTTP_PROVIDER, FooService, ServiceNeededByFoo])
This initializes SomeComponent and puts the providers for the three services in there. This way SomeComponent can resolve FooService in its own constructor, as well as FooService can resolve ServiceNeededByFoo since it looks it up in SomeComponent (the parent).
If I define
providers: [HTTP_PROVIDER, FooService, ServiceNeededByFoo]
inside SomeComponent, I can initialize it like so:
bootstrap(SomeComponent)
So from what I understand these should be completely the same, a new instance for FooService and ServiceNeededByFoo on the level of SomeComponent is created and shared with all children of SomeComponent that also need FooService. If it's the same, is there any preferred/recommended way?
The second question is now, how can I share a single instance of FooService across components that are not in the same DI hierarchy:
bootstrap(SomeComponent1, FooService)
bootstrap(SomeComponent2, FooService)
where the FooService should be the same instance. Something like:
var foo = new FooService();
foo.expensiveInit();
bootstrap(SomeComponent1, provide(FooService, {instance: foo}))
bootstrap(SomeComponent2, provide(FooService, {instance: foo}))