我有一个属性:
public rows: any[];
Run Code Online (Sandbox Code Playgroud)
然后我在构造函数中填充这个数组:
this.rows.push({"id": 1, "subject": "Subject", "scope": "Learn basics", "hours": 2});
Run Code Online (Sandbox Code Playgroud)
在模板中,我像这样迭代数组:
<tr *ngFor="let p of rows; let i = index;">
Run Code Online (Sandbox Code Playgroud)
所以,如果我从数组中删除元素:
this.rows.splice(this.rows.length - 1, 1);
Run Code Online (Sandbox Code Playgroud)
它不会改变模型,我像以前一样看到数组中的一行。
如何在Angular 2中以Promise类型返回数据?
使用此功能,我需要返回数据作为promise类型:
private getStudyPeriods(): Promise<CurrentPeriod> {
let data = [];
return data;// here
}
Run Code Online (Sandbox Code Playgroud)
我尝试了这样的最后一个变体:
public getStudyPeriods(): Promise<Period[]> {
return this.education.get(7).then((data: any) => {
if (!data.hasOwnProperty('errors')) {
this.periods = data;
return new Promise((resolve, reject) => {
resolve(this.periods);
});
}
})
}
Run Code Online (Sandbox Code Playgroud)