【发布时间】:2017-04-30 23:09:30
【问题描述】:
我正在尝试使用 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 !!!";
}
}
所以,我不确定构造函数调用何时结束。
我想在构造函数调用完成时捕获事件。
来自这两个链接-
我知道了-
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的方法来控制你的组件流?似乎构造函数和初始化函数本质上不是异步的
-
我想在加载时有数据
标签: angular components angular2-directives angular2-services angular-components