both this child calles in the parent i have challange that in child1 dropdown selection change i need to call api in child2 how to do it <app-newhire-header-details [parentData]="parentData" [formData]="combinedFormData.masterHeader" (formDataChange)="onFormDataReceived('masterHeader', $event)"></app-newhire-header-details> <app-personal-tab [formFields]="tabFormFields[2]" [tabKey]="2" [formData]="combinedFormData.personal" (formDataChange)="onFormDataReceived('personal', $event)" (nextTab)="nextTab()" (prevTab)="prevTab()"> </app-personal-tab>
For child-to-child communication, where a change in one child component triggers an action (like an API call) in a sibling child component, the standard Angular approach is for the parent component to act as a mediator
. A shared service is a more robust solution for complex scenarios or when components are not direct siblings. Solution 1: Parent as a mediator
This is the most straightforward and recommended approach for components that are direct siblings under the same parent.
1. Emit an event from the first child (
In
app-newhire-header-details)In
newhire-header-details.component.ts, emit an event when the dropdown selection changes. 2. Listen to the event in the parent component
The parent component listens for the event from the first child.
The parent component listens for the event from the first child.
In
parent.component.ts, implement the event handler and use it to update an input property on the second child.3. Watch for changes in the second child (
The second child needs to receive the value from the parent and take action when it changes.
app-personal-tab)The second child needs to receive the value from the parent and take action when it changes.
No comments:
Post a Comment