Initially, I have a page form with preset values and buttons for navigating to the next or previous items.
Upon initialization in ngOnInit, an observable provides me with a list of 3 items as the starting value - sometimes it may even contain 4 items.
However, before proceeding to the next page, it is crucial that I click on a button triggering the function calculate()
. This function makes a request which updates the observable List to include 4 items.
When I click on the next button onNextButton()
, I wish to compare the initial value with the current one to determine if any changes have occurred. Unfortunately, my current implementation doesn't store the initial value properly, resulting in issues when comparing values upon clicking the next button.
This is my code:
export class PageComponent implements OnInit {
questions$: Observable<any[]>;
hasChange: boolean;
ngOnInit() {
// GETTING INITIAL VALUE
this.questions$.subscribe((quest: any[]) => {
this.hasChange = quest.length > 3 ? true : false
});
}
calculate() {
// make a request
// the observable will now contain 4 items.
}
onNextButton() {
if (hasChange) {
// submit()
} else {
// navigate()
}
}
}
Therefore, I am looking for a way to retain the previous value of the Observable and store it in a variable, or alternatively, how to detect any changes effectively.
I have attempted using `behavioursubject` and `pairwise` from RxJS, but I am uncertain about their correct usage in my case.
Thank you in advance for your help!