-3

I want to store data from an API call in a variable and then use it out of the subscription.

GetAges(): Observable<string[]> {
    var url = this.SharedURL + '/ageGroupLocal?lang=' + this.lang;

    return this.http.get<string[]>(url);
  };

This is my service and I want to store the response in a variable and use it in a component.

Manuchar
  • 1
  • 1
  • 3
    So subscribe to the observable. See e.g. https://angular.io/tutorial/tour-of-heroes/toh-pt4#observable-data. More generally see https://stackoverflow.com/q/14220321/3001761. – jonrsharpe May 11 '23 at 08:51

1 Answers1

-1

You should subscribe to this GetAges() in your component and then save it to the private or public variable inside the component and that's it. You can use it across the class

  • If i assign it to a local variable, variable will remain udefine, only inside subscription it will have value – Manuchar May 11 '23 at 15:05