I'm facing a challenge. Is it possible to resolve a component manually with just its name known?
Let's say I have a string variable that holds the name of a component, like "UserService".
I've been exploring Injector
and came across methods like resolve()
and resolveAndCreate()
in the documentation (https://angular.io/docs/ts/latest/api/core/Injector-class.html)
However, the issue is I don't have the actual Type.
Any insights on this would be greatly appreciated.
Cheers,
Steve
UPDATE: Attempted the following:
System.import("path/to/service").then(p=>{
var injector = Injector.resolveAndCreate([p[this.serviceName]]);//works
var serviceInstance = injector.get(p[this.serviceName]); // Fails with "No provider for Http! (UserService -> Http)".
});
I do have Http
available, as it has been provided during bootstrap and works well for other parts of the application.
bootstrap(AppComponent, [
ROUTER_PROVIDERS, Http, HTTP_PROVIDERS, UrlBuilderService,
provide(LocationStrategy, { useClass: HashLocationStrategy }),
provide('notification', { useClass: NotificationService })
]);
Any thoughts?
Additional Update:
I adjusted the call to resolveAndCreate
like this:
var injector = Injector.resolveAndCreate([p[this.serviceName], Http]);
but encountered an error:
No provider for ConnectionBackend! (UserService -> Http -> ConnectionBackend)
So, I modified it again:
var injector = Injector.resolveAndCreate([p[this.serviceName], Http, ConnectionBackend]);
this led to more missing components errors.
Eventually, I did this:
var injector = Injector.resolveAndCreate([p[this.serviceName], HTTP_PROVIDERS]);
and now everything is working as expected.
The confusion lies in why these components are not automatically resolved when they were provided during bootstrap.