I’ve read somewhere that @Injectables are suppose to be singleton. I created one and load a <2mb json file and saved it on a variable. The file is loaded at @App init but when I try to retrieve the data on a @Page its always empty. So I resorted on calling the loadFile() on constructor everytime I want to use the data. Is this the correct behaviour of @Injectable?
@Injectable
export class StaticFileService {
private _persons: Person[] = new Array<Person>();
constructor(private _http:Http){}
loadFile(){
this.http.get('data/myFile.json')
.map(res => res.json())
.subscribe(res => {
for (let j in res) {
let p = new Person(res[j].name, res[j].area);
this._persons.push(p);
}
})
}
get persons():Person[] { return this._persons; }
}