For my app, I need to use List.json to consctruct a list (the core of my app). To share this list between all my components, I've decided to create a service.
In this service, I call my json file in the constructor, and use a getter to get this list in others components. But when I call the getter, the constructor has not finished (http get is a little bit slow) and it returns nothing.. (with a little timeout, everyting works fine but it's dirty)
Is it possible to call the getter once the constructor is finished ?
Thanks in advance !
My service:
import {Injectable} from "angular2/core";
import {Http, Response, HTTP_PROVIDERS} from 'angular2/http';
@Injectable()
export class listService {
private list;
constructor(http: Http) {
http.get('app/List.json')
.map(res => res.json())
.subscribe(
data => this.list = data
);
}
getValue(){
return this.list;
}
}
My component which display the list:
import {listService} from './listService';
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES, Router} from 'angular2/router';
import 'rxjs/add/operator/map';
@Component({
selector: 'list',
template: `
<h2>Formations </h2>
<ul><li (click)='showItem(item)' *ngFor="#item of list"><span >{{item.Method}}</span></li></ul>
`,
directives: [ROUTER_DIRECTIVES]
})
export class List {
public list: Object[];
constructor(private listService:listService, private _router: Router) {
this.list = this.listService.getValue();
}
showItem(item){
this._router.navigate( ['Item', { id: item.id }] );
}
}