Дочерний ящик, который я создаю изнутри родителя на лету, содержит кнопку закрытия, которая запускает функцию закрытия, которая на самом деле является EventEmitter, сообщение которого должно как-то достигнуть родителя, чтобы уничтожить дочерний компонент с помощью this.child.destroy. (); "
Код в родительском элементе для создания дочернего элемента:
childComponentRef: any;
@ViewChild('childComponentcontainer', {static : false, read: ViewContainerRef }) entry: ViewContainerRef;
createchildComponent(message:any) {
this.entry.clear();
const factory = this.resolver.resolveComponentFactory(childComponent);
this.childComponentRef = this.entry.createComponent(factory);
this.childComponentRef.instance.data = message;
}
closechildComponent($event){
console.log("dest")
//this.childComponentRef.destroy();
}
и html
<template #childComponentcontainer (close) = "closechildComponent($event)"></template>
или
<template #childComponentcontainer></template>
и дочерний элемент
onClose() {
this.close.emit("close");
}
Дочерний объект отображается, но при нажатии кнопки закрытия ничего не происходит. Как я могу это исправить?
Обычно я бы использовал
<child
(close) = "closebox($event)"
[data] = "data">
</child>
, но здесь я не могу добавить (закрыть) привязку, потому что я создаю компонент на лету
https://angular-excmcj.stackblitz.io
Спасибо!