Solution 2: Shared service (recommended for complex applications)
For more complex applications, a shared service with an RxJS Subject or BehaviorSubject is a cleaner solution for decoupled communication.
1. Create a shared service The service will hold an observable that other components can subscribe to.
typescript
// dropdown.service.tsimport { Injectable } from'@angular/core';
import { Subject } from'rxjs';
@Injectable({
providedIn: 'root'
})
exportclass DropdownService {
private dropdownSubject = new Subject<any>();
// Observable for other components to subscribe to
dropdownValue$ = this.dropdownSubject.asObservable();
// Method to be called by child1 when the dropdown changes
sendDropdownValue(value: any) {
this.dropdownSubject.next(value);
}
}
Use code with caution.
2. Send the value from the first child (app-newhire-header-details) In newhire-header-details.component.ts, inject the service and call its sendDropdownValue() method.
3. Subscribe to the event in the second child (app-personal-tab) In personal-tab.component.ts, inject the service and subscribe to the observable to get the latest value.
No comments:
Post a Comment