Looking to parse a JSON file and create a settingsProvider.
This is how I am attempting it:
import {Http} from "angular2/http";
import {Injectable} from "angular2/core";
@Injectable()
export class SettingsProvider{
url: string = "";
constructor(http: Http){
http.request('js/app/config/config.json').map(data => data.json()).subscribe(data => this.url = data.url);
}
getUrl():string {
return this.url;
}
}
Encountering an error like this:
https://i.stack.imgur.com/bOSN7.png
So, my initial question is - why is this happening?
Second question:
When I try the following approach:
http.request('js/app/config/config.json').subscribe(data => {
this.url = data.json().url;
});
this doesn't refer to the class but to the Subscriber instance. Why does this occur? I believed that using fat-arrow lambda in TypeScript should prevent such strange closures.