如何使用带角度2和环回的承诺?

时间:2016-06-17 05:52:42

标签: elasticsearch angular loopback

这里显示了在angular2框架中使用loopback完成的项目。 我想使用promise和使用loopback检索的数据。

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视图中。 怎么做?

1 个答案:

答案 0 :(得分:1)

您未在Promise中返回getAllData()。你可以这样试试:

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>
相关问题