Saturday, May 2, 2020

How to call another component function in angular ( Child > Parent )

Hello,
First, what you need to understand the relationships between components. Then you can choose the right method of communication. I will try to explain all the methods that I know and use in my practice for communication between components.
  • Sharing Data via ViewChild
Property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the view DOM. If the view DOM changes, and a new child matches the selector, the property is updated. View queries are set before the ngAfterViewInit callback is called.
ViewChild allows one component to be injected into another, giving the parent access to its attributes and functions. One caveat, however, is that child won’t be available until after the view has been initialized. This means we need to implement the AfterViewInit lifecycle hook to receive the data from the child.
parent.component.ts
child.component.ts
  • Sharing Data via Output() and EventEmitter
Another way to share data is to emit data from the child, which can be listed by the parent. This approach is ideal when you want to share data changes that occur on things like button clicks, form entries, and other user events.
parent.component.ts
child.component.ts

0 comments:

Post a Comment