我正在尝试使用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
我想在加载时有数据 –