I\'m trying to get a button click in one component to put focus on an element on another component. (Frankly, I don\'t understand why this must be so complex, but I have not bee
For child-parent communication it is better to use a Subject or BehaviorSubject. This is an example from the angular guide.
https://angular.io/guide/component-interaction
@Injectable()
export class MissionService {
// Observable string sources
private missionAnnouncedSource = new Subject();
private missionConfirmedSource = new Subject();
// Observable string streams
missionAnnounced$ = this.missionAnnouncedSource.asObservable();
missionConfirmed$ = this.missionConfirmedSource.asObservable();
// Service message commands
announceMission(mission: string) {
this.missionAnnouncedSource.next(mission);
}
confirmMission(astronaut: string) {
this.missionConfirmedSource.next(astronaut);
}
}
If you have an event that just signifies something has occurred or completed - and has no actual data associated with it - you can use a Subject
. This makes the signature of next()
cleaner and you don't need to provide a dummy value for the sake of it.
Eg.
windowClosed = new Subject();
windowClosed.next()
will emit the event