Using Injector.create() to dynamically create injector for embedded view is not working when...

Discussão em 'Angular' iniciado por Raziel, Outubro 23, 2024.

  1. Raziel

    Raziel Guest

    I want to dynamically create the injector for embeddedView and for that purpose I use Injector.create() method:

    const myInjector = Injector.create({
    providers: [
    {
    provide: MyService,
    useFactory: () => new MyService(),
    },
    ],
    parent: this.vcr.injector,
    });


    then I create embedded view

    const view = this.template.createEmbeddedView(
    null,
    myInjector
    );


    the problem is the template components do not get the services of this.vcr.injector but they do get istance of MyService

    but when I create the embeddedView like this:

    const view = this.template.createEmbeddedView(
    null,
    this.vcr.injector
    );


    The this.vcr.injector works and templates get the instances of services but I need the ability to create providers for some templates and for some not.

    Continue reading...

Compartilhe esta Página