1

I have a parent component which loads a child component using DCL loadnextto location>.

When I click on a button in the parent component I want to raise the event in Child component.

Earlier I used to raise the event using viewchild but now it is showing an error, I used to raise events in child components from parent using viewchild in beta.12.

Somebody tell me what changes should I make to raise the events in the child component in beta-16.

This is my demo http://plnkr.co/edit/LKWr27zwa2IutryOStgR?p=preview

@ViewChild(Child) childcomp: Child;
//This is by button event used to raise an event in child component
    GetData(){
        this.childcomp.raiseEvent();
    }

Thanks!

Elydasian
  • 2,016
  • 5
  • 23
  • 41
abhilash reddy
  • 1,546
  • 8
  • 31
  • 53

1 Answers1

1

One way to do it is to access cmpRef.instance that you get from loadNextToLocation()

  ngAfterViewInit() { 
    this.dcl.loadNextToLocation(Child, this.viewport)
    .then(cmpRef => {
      console.log(cmpRef);
      this.cmpRef = cmpRef;
    });
  }
<button (click)="cmpRef.instance.raiseEvent()">Button</button>

Plunker example

DynamicComponentLoader is deprecated. See Angular 2 dynamic tabs with user-click chosen components for an example how to use ViewContainerRef.createComponent() instead.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567