如何将 promise 与 angular 2 和环回一起使用?

How to use promise with angular 2 and loopback?

这里展示了在带有回环的angular2框架中完成的项目。 我想将 promise 与使用环回检索的数据一起使用。

data.component.ts

ngOnInit () {
this.dataService.getAllData()
.then(response => {this.data.push(response);});
}

data.service.ts

public getAllData(): any {
    this.my_model.find()
    .toPromise()
    .then((res : Response) => res);

}

我想将此数据插入 html 视图。 如何做到这一点?

您不会在 getAllData() 中返回 Promise。您可以这样尝试:

data.service.ts

public getAllData(): any {
    return this.my_model.find().toPromise();
}

data.component.ts

ngOnInit () {
    this.dataService.getAllData()
    .then(response => { this.data.push(response); });
}

您可以在模板中的某处使用此数组:

<div *ngFor="let item of data">
    <span>{{ item.id }}</span>
</div>