Issue
I'm searching how to call a method of a component called dynamically by ngComponentOutlet.
Inside my Caller
this.dialog.open(FormDialogComponent, {data: {body: ChildComponent}});
FormDialogComponent
export interface FormDialogData {
title: string,
body: Type<any>
}
export class FormDialogComponent {
@ViewChild(ChildComponent) child!: ChildComponent;
constructor(public dialogRef: MatDialogRef<FormDialogComponent>, @Inject(MAT_DIALOG_DATA) public data: FormDialogData) {}
save(){
this.child.save(); // trying to call save() method of childComponent not working
}
}
How can I call call the save method of my child?
ADD
I forget to se the html part
<ng-container [ngComponentOutlet]="data.body" #child></ng-container>
Solution
I finally fixed it
the caller stay the self
in My formDialogComponent.ts
@ViewChild('formComponent', {read: ViewContainerRef}) viewContainerRef!: ViewContainerRef;
also in formDialogComponent.ts
// Create my component
this.childComponent = this.viewContainerRef.createComponent(this.data.body).instance;
in formDialogComponent.html
<button mat-raised-button color="primary" (click)="childComponent.save()"><mat-icon>add</mat-icon></button>
Answered By - Python
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.