How do I use Angular 19 Observer for HTTP Requests?

Discussão em 'Angular' iniciado por user28348427, Março 29, 2025.

  1. user28348427

    user28348427 Guest

    sorry if this was asked already, I couldn't find a similar question that had a satisfactory answer or had an answer that wasn't deprecated. I'm not really sure how to word what I am trying to do in any technical way, I am still very new to web development.

    Essentially I am trying to assign data from a GET request to a variable. I understand that an HttpClient GET request returns an Observable that must be subscribed to. The following works, it prints the correct data (a Json object):

    this.get("").subscribe(response => {console.log(response)});


    However when I try to assign response to a variable:

    let raw; //have also tried var as I thought it might be related to scope?
    this.get("").subscribe(response => {raw = response});
    console.log(raw) //returns undefined

    let raw = this.get("").subscribe(response => response); //have also tried => response.data with same result
    console.log(raw) //returns a weird object that I assume is the observer?


    What is the best way to do this? Thanks in advance.

    Continue reading...

Compartilhe esta Página