abhilash reddy
abhilash reddy

Reputation: 1586

How to raise event in a component which is created using DCL loadnexttolocation()?

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!

Upvotes: 1

Views: 225

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657308

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.

Upvotes: 1

Related Questions