我正在嘗試使用Angular 2做一個簡單的工作。Angular 2 - 構造函數調用後的生命週期事件
我想在組件的構造函數調用完成後捕獲事件。 我想提出一個網絡調用構造函數像這個 -
import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { Profile } from '../../dataModel/Profile.ts'; //Data Model
@Component({
selector: 'profile-list',
template: require('./profileList.component.html'),
styles: [require('./profileList.component.css')]
})
export class ProfileListComponent
{
public profiles: Profile[]; //Can be accessed in view
public loadingMessage: string = "Loading Data ..";
constructor(http: Http)
{
http.get('/api/Profile/Get?currentPageNo=1&pageSize=20').subscribe(result =>
{
this.profiles = result.json();
console.log(result);
});
}
ngOnInit()
{
this.loadingMessage = "No Data Found !!!";
}
}
所以,我不知道什麼時候構造函數調用結束。
我想在構造函數調用完成時捕獲事件。
從這2鏈接 -
我才知道這個 -
export class App implements OnInit{
constructor(){
//called first time before the ngOnInit()
}
ngOnInit(){
//called after the constructor and called after the first ngOnChanges()
}
}
所以,在我的constructor
呼叫完成後調用onInit
是否正確?
請幫幫我。
在此先感謝您的幫助。
爲什麼不能像'getProfile'這樣的基於promise的additinal方法來控制組件流?似乎constrcutors和initalizors在其性質上不是異步的 – VadimB
我想在加載時有數據 –