yeiniel
yeiniel

Reputation: 2456

angular 2 (2.0.0-rc1) how to create a child injector on code

I need to call the following code with a new injector created to add providers dynamically:

var componentRef = componentFactory.create(
    injector, undefined, `#${this.id}`
); 

I can't find an example on the internet on howto create a child injector whose parent is the component injector. Any ideas?

I have tried to inject a ReflectiveInjector (so i can call resolveAndCreateChild method) into may component but an exception is raised at runtime saying there is no provider for that class.

Upvotes: 0

Views: 269

Answers (1)

StocksR
StocksR

Reputation: 993

inject Injector and then

        let resolvedProviders = ReflectiveInjector.resolve([new Provider('node', { useValue: node })]);
        let child = ReflectiveInjector.fromResolvedProviders(resolvedProviders, this.injector);

Upvotes: 1

Related Questions