0
votes

On next click emitting value to observable able to get but it getting called n number of times first it is getting empty value next getting the object. so i need to write a if condition and in that if value is not empty then perform some logic. so is there any way to get the finally completed value so tried rxjs operator like last take(1) and behaviour subject but none of them working attach code snippet is anything is wrong in writing observable

savePersonalInfo$ = new Subject<any>();     
get savePersonalInfo() {
    return this.savePersonalInfo$.asObservable();
}

my service file looks like this where creating a subject tried with behaviour subject but no luck.

onClickedNext(){
  // this.isContinueClicked = true;
  // this.shared.coapplicantForm = this.register;
  this.shared.isFromNext = true;
  this.shared.savePersonalInfo$.next({value: 'clicked'});

}

on next click event where emitting the value so it is from navigation component.

this.shared.savePersonalInfo.subscribe(resp => {
 if (resp.value != "") {
 }
});

code trying to get the emitted value here it needs to get the latest value initially its getting empty this is other component where on next click need to save data in personal component so writing if condition whether data is available so in other place in code it sends object how to differentiate in that case since it gets object.

2
It looks like you could use just filter(Boolean), skip(1) or filter(whatever logic) operators. - martin
@martin without those filters is there any approach if there is big object every time getting from api just a single key change in the object how to do in that case - UI DEV
can you share your problem here in stackblitz? - GaurangDhorda
if i understand you correct, you subscribe in your last snippet whenever there is a click event. This could result in you emitting your value before subscribing. You should definetly post a complete example in a stackblitz link. You possibly want to use the shareReplay(num) operator but it is difficult to say - Chund

2 Answers

0
votes

You can use skipWhile operator to check if the value emitted is empty. The following code block might help you.

this.shared.savePersonalInfo.pipe(skipWhile(value => !value)).subscribe(res => {
 console.log(res);
});
0
votes

You need to use ReplaySubject. It emits last value to every new subscriber