1

I am adding dynamic components to div in angular using createComponent method

let coolComponentRef = this.formControls?.createComponent(CoolComponent);

Child components site inside parent components and there are few events which i want to send to parent components. I do this using @Output on child component.

@Output()
propertiesEvent = new EventEmitter<Properties>();

and its usage is

<cool-component (propertiesEvent)="itemSelected($event)"></cool-component>

full example:
<div id="formControls" #formControls>
<cool-component (propertiesEvent)="itemSelected($event)"></cool-component>
</div>

However, my use case is that the child elements are added dynamically using createComponent, so how can i add (propertiesEvent) to dynamically created component.

EDIT: Found this link usefull Angular4 communication between child dynamically created component to parent, is there other simpler way exists?

silentsudo
  • 6,730
  • 6
  • 39
  • 81
  • You can either (as the linked question states) subscribe to the `EventEmitter` prorammatically right after creating the component, or you could write a shared service that the dynamically created components can interact with if needed. – Cuzy Apr 13 '23 at 11:30
  • I imagine you can use a service. – Eliseo Apr 13 '23 at 13:26

0 Answers0