Compare emitted values from the same Request

Discussão em 'Angular' iniciado por Sergej Bjakow, Fevereiro 27, 2025.

  1. In my angular component I fetch a list of objects via http-Request and render that data in a List and set some additional configdata based on that list.

    In the end I have a "proceed" button that first needs to check again wether or not the initially fetched data has not changed meanwhile on serverside.

    So I need to send the same request again and check wether or not the list objects has changed in any way (either the amount of objects or a certain value in one specific object).

    So the question is what is a best practice to retrigger the same request that emits to the same observable and check if the new emitted value is different from the last one?.

    actually its probably a nobrainer for most peaple, because I could do something like this

    currentValue: any;


    ngOnInit(): void {
    this.httpRequest('get').subscribe(val => {
    this.currentValue = val
    })
    }


    checkAgain(){
    this.httpRequest('get').subscribe(val => {

    if(val.length !== this.currentValue.length){
    alert('something changed')
    }
    })
    }


    But this looks like a clueless beginner's approach. is there a more elegant way using BehaviorSubject, or is this approach just fine?

    Continue reading...

Compartilhe esta Página