2016-06-01 32 views
0

我想知道onInit是如何工作的,尤其是在調用某些服務時。這是我的主要成分:Angular2,使用主要組件的onInit來調用服務

@Component({ 
    selector: "app", 
    templateUrl: "app/app.html", 
    directives: [ROUTER_DIRECTIVES] 
}) 
@RouteConfig(){ 
... 
} 
export class AppComponent implements OnInit { 
    constructor (private _fooManager: FooManager) {} 

    ngOnInit() { 
     this._fooManager.fetchAllFoos() // Do a web service call to fetch some data and store them inside the FooManager 
    } 
} 

FooManager的注射來自我bootstrap.ts)

然後我的所有其它部件都採用了FooManager的數據(尤其是在他們的模板)。我想知道是否必須檢查在fetchAllFoos中完成的呼叫的狀態,或者ngOnInit是否能夠保證數值滿載?

如果不是,我想一個解決方案是給出一個承諾,然後檢查承諾狀態。

回答