I need to capture the output from a rendered component using ViewChild. The content of ViewChild is displayed after an ngIf condition is met.
Here is the template code:
<div *ngIf="isModalVisible" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<!-- ... -->
</div>
<div class="modal-body">
<ng-template #dialogHost (saveModel)="setModel($event)"></ng-template>
</div>
</div>
</div>
</div>
This is the TypeScript file:
import { ChangeDetectorRef, Component, ComponentFactoryResolver, Input, ViewChild, ViewContainerRef } from '@angular/core';
import { DialogContentItem } from 'src/app/models/dialog/content/dialog-content-item';
@Component({
selector: 'app-dialog-preparator',
templateUrl: './dialog-preparator.component.html',
styleUrls: ['./dialog-preparator.component.css']
})
export class DialogPreparatorComponent {
isModalVisible = false;
@Input() dialogContent: DialogContentItem;
@ViewChild('dialogHost', {static: false, read: ViewContainerRef}) dialogHost: ViewContainerRef;
showModal() {
this.isModalVisible = true;
this.changeDetector.detectChanges();
this.renderComponent();
}
renderComponent() {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.dialogContent.component);
this.dialogHost.clear();
const componentRef = this.dialogHost.createComponent(componentFactory);
(componentRef.instance as DialogContentInterface).data = this.dialogContent.data;
}
setModel(model: any) {
// Need to emit the model
}
}
This is the ProcutCreateEditComponent:
<app-dialog-preparator [dialogContent]="userDialogContent"></app-dialog-preparator>
The TypeScript code for this component:
import { Component } from '@angular/core';
import { UserCreateEditComponent } from 'src/app/components/user/user-create-edit.component';
@Component({
selector: 'app-product-create-edit',
templateUrl: './product-create-edit.component.html',
styleUrls: ['./product-create-edit.component.scss']
})
export class ProductCreateEditComponent {
userDialogContent: DialogContentItem = new DialogContentItem(UserCreateEditComponent, {});
constructor() {}
// ...
}
And here is the UserCreateEditComponent's TypeScript code:
import { Component, Output } from '@angular/core';
@Component({
selector: 'app-user-create-edit',
templateUrl: './user-create-edit.component.html',
styleUrls: ['./user-create-edit.component.scss']
})
export class UserCreateEditComponent {
@Output() saveModel: EventEmitter<T> = new EventEmitter();
user = new User();
constructor() {}
save() {
this.saveModel.emit(this.user);
}
}
In the ProcutCreateEditComponent, I instantiate the UserCreateEditComponent and pass it to the DialogPreparatorComponent. The UserCreateEditComponent shows up in a dialog within the DialogPreparatorComponent. When I click on the save button in the UserCreateEditComponent, the saveModel event should be emitted to the DialogPreparatorComponent. However, I'm encountering the following error message:
Event binding saveModel not emitted by any directive on an embedded template.
Make sure that the event name is spelled correctly and all directives are
listed in the "@NgModule.declarations". ("
</div>
<div class="modal-body">
<ng-template #dialogHost [ERROR ->](saveModel)="setModel($event)"></ng-template>
</div>
</div>
It seems the ng-template does not have the saveModel output defined. How can I capture the output from the UserCreateEditComponent at this point?