-1

So is it possible to subscribe to more than one json variables?

 this.http.get(this.api_url + "preference")
  .map(response => response.json())
  .subscribe(res => this.limit = res.limit, res => this.sources = res.sources);

In this example, the sources is an array of strings. If you do it like this, only limit will be set.

my json:

{   "sources":["examplePage"],
    "limit":5
}

1 Answers1

2

You can do multiple operations in the subscribe body by using curly braces {} to define the body of the function and then assign the two variables like so:

this.http.get(this.api_url + "preference")
    .map(response => response.json())
    .subscribe(res => {
        this.limit = res.limit;
        this.sources = res.sources;
});

As an aside the way subscribe is written in your question the limit will be set when the http request succeeds and the sources will be set when the request fails. subscribe(/* success */, /* error */, /* finished */)

Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51