load(): Observable<string> {
return this.http.get(url).map((rsp) => {
// it would be best here if you had an actual interface type for what is coming from the server to make typos harder
return rsp.json().items[0].str;
});
}
I’m deliberately not setting this.data here, because virtually always you don’t really want to do that, as you will get subtle bugs with stale data. If you are going to do it, don’t make it a simple value. Make it a ReplaySubject and do this:
…and in this idiom, you probably don’t even want to be returning anything from load(), because everybody who’s interested in the value should be subscribing to the data Subject, which will be automatically updated. load() then becomes simply a trigger to update data.