Skip to content
Advertisement

What is the correct way to call a method of the host or parent component from a dynamically added component?

I want to know the correct way to call the greet() method declared in the host component from a dynamically added component

src/app/app.component.ts

JavaScript

src/app/ad-host.directive.ts

JavaScript

src/app/app.component.html

JavaScript

In my case there are three components that are dynamically added in all of them I need to call a method in the host component, for example in component One

src/app/application/one/one.component.ts

JavaScript

src/app/application/one/one.component.html

JavaScript

Update 1

I share the repository of the listed examples to facilitate your collaboration

https://github.com/ilmoralito/add-components-dynamically-demo-app

Thanks for your comments

Advertisement

Answer

You can add an output() in OneComponent then you subscribe in your AppComponent like below:

JavaScript

src/app/app.component.ts

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement