1

I have a component where I am dynamically injecting another component. Please refer the code shown below:

getID() {
    const componentRef = this.componentFactoryResolver
        .resolveComponentFactory(CodesComponent).create(this.injector);
    componentRef.instance.terms = this.terms;
    this.appRef.attachView(componentRef.hostView);
    const domElem = (componentRef.hostView as EmbeddedViewRef<any>)
        .rootNodes[0] as HTMLElement;
    $(domElem).insertAfter($(event.target).closest('tr'));
}

Also there is a function in my component:

sayHello() {
    console.log('hi');
}

And my CodesComponent looks like below:

<p (click) = "sayHello()"></p>

Now question is how can I call sayHello() function from dynamically created component?

7
  • Is the sayHello function inside your parent component or dynamically created component? Commented Mar 26, 2018 at 13:53
  • 1
    You can't use normal DOM manipulation to create Angular components since it doesn't perform the necessary compilation steps. See: angular.io/guide/dynamic-component-loader Commented Mar 26, 2018 at 13:53
  • @ManzurKhan sayHello function is in my parent component. Commented Mar 26, 2018 at 13:53
  • Hi @TavishAggarwal, would you please show your code for the dynamic component, CodesComponent? Commented Mar 26, 2018 at 14:02
  • @Narm <p (click) = "sayHello()"></p> this line of code I have in my dynamic component. Commented Mar 26, 2018 at 14:03

1 Answer 1

3

For your use case I would recommend using Angulars dependency injection to inject the parent component into your dynamic component.

Here is a working StackBlitz demo for you.

Code in your parent component

import {
  Component, ViewChild, AfterContentInit, ComponentFactoryResolver,
  Compiler, ViewContainerRef, NgModule, NgModuleRef
} from '@angular/core';


import { BrowserModule } from '@angular/platform-browser';


@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterContentInit {
  @ViewChild('vc', { read: ViewContainerRef }) _container: ViewContainerRef;
  private cmpRef;

  constructor(
    private componentFactoryResolver: ComponentFactoryResolver,
    private compiler: Compiler,
    private _m: NgModuleRef<any>) { }

  ngAfterContentInit() {
    this.getID();
  }

  public sayHello(): void{
    alert("Hello!");
  }

  private getID(): void {
    @Component({
      template: `<h2>This is a dynamic component</h2>
      <p (click)="_parent.sayHello()">Click me!</p>`
    })
    class DynamicComponent {
      constructor(public _parent: AppComponent) { }
    }
    @NgModule({
      imports: [
        BrowserModule
      ],
      declarations: [DynamicComponent],
    }) class DynamicModule { }

    const mod = this.compiler.compileModuleAndAllComponentsSync(DynamicModule);
    const factory = mod.componentFactories.find((comp) =>
      comp.componentType === DynamicComponent
    );

    this.cmpRef = this._container.createComponent(factory);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.