as shown in the below posted code, i emit an event as shown in section emitting an event
. the value to be emitted is a string and the log-statement confirm that as shown in the below posted screen-shot.
the problem i am facing, is as i am registered to listen to the emitted event as shown in section ngOnInit
below, i receive several values for the emitted value param
as shown in the screen-shot.
in other words, assume that the event in emitting an event
section is emitted on button clicks, for the first click
console.log("this.selectedSite.id:",this.selectedSite.id) displays 202207070658 which is the most recent emitted value. this line behave correctly as is displays the most recent value as expected
but the problem is in the ngOnInit
section- For the first click "first emittance to the event",
console.log("******************selectedSiteID:",this.selectedSiteID) displays 202226081143
after clicking two times, it displays: 202226081143 and 202228011057
after clicking four time, it displays the values shown in the screen shot
i would like to receive only the most recent emitted value only. how can i achieve that
service:
export class FromSiteMapToVisOptionsService {
private broadcastEmitterToAckSelectedSiteID:Subject<string> = new ReplaySubject<string>();
constructor() { }
public emitToAckSelectedSiteID(param) {
this.broadcastEmitterToAckSelectedSiteID.next(param)
}
public getBroadcastEmitterToAckSelectedSiteID() {
return this.broadcastEmitterToAckSelectedSiteID.asObservable()
}
}
ngOnInit:
this.subscriptionOnAckSelectedSiteID = this._FromSiteMapToVisOptionsService.getBroadcastEmitterToAckSelectedSiteID().subscribe((param:string)=>{
this.selectedSiteID = param
this.storedSelectedSiteID = this._PersistStateService.getItem(this.keySelectedSiteID)
console.log("******************selectedSiteID:",this.selectedSiteID)
})
emitting an event
// assume it will be emitted on button clicks
this.subscriptionEventEmitterToRequestSelectedSiteID = this._FromVisOptionsToSiteMapService.getBroadcastEmitterToRequestSelectedSiteID().subscribe(()=> {
console.log("this.selectedSite.id:",this.selectedSite.id)//displays the value of this.selectedSiteID only once.this.selectedSiteID contains only one value as shown in the screen-shot, its value is displayed once
this._FromSiteMapToVisOptionsService.emitToAckSelectedSiteID(this.selectedSite.id)
})
img