I have a behavior subject in main component when the user click on update button it will emit true/false based on the condition.
That behavior subject is subscribed in another component and based on that true/false I need to show or hide rows.
Problem is behavior subject variable is emitted only one time in main but subscribed multiple times in other component.
my code : in service.ts
public sendToRenderer = new BehaviorSubject<boolean>(true);
public recievedData=this.sendToRenderer.asObservable();
changeData(data: boolean) {
this.sendToRenderer.next(data)
}
tried with subject also like below:(but same response)
public sendToRenderer = new Subject<boolean>();
public recievedData=this.sendToRenderer.asObservable();
changeData(data: boolean) {
this.sendToRenderer.next(data)
}
in main component.ts
onUpdate(e: any) {
this.uploadService.changeData(this.isRemarksUpdated)
//this.isRemarksupdated is comning as true/false from other method
}
in other component.ts
ngOnInit(): void {
this.subscripition= this.uploadService.recievedData.subscribe((data)=>{
this.ispreFundEdited=data
})
}
when I'm using this.ispreFundEdited in any other method coming as undefined for the first time then coming true/false
here data getting multiple times like below (false/true)
what I have tried :
I have tried with using pipe like ".pipe(take(1)).subscribe(()=>{})", but no luck.

this.ispreFundEditedcan beundefinedif you use it before the assignment insubscribe((data) => ...)happens.Possible reasons why you see the data multiple times:
onUpdatemultiple times.shareReplayorsharein your service depending on your use case:public recievedData$ = this.sendToRenderer.asObservable().pipe(shareReplay(1));