简体   繁体   中英

How to use promise with angular 2 and loopback?

Here shows the project done in angular2 framework with loopback. I want to use promise along with data retrieved using 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);

}

I want to interpolate this data to the html view. How to do this?

You're not returning a Promise in your getAllData() . You could try it like this:

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); });
}

and somewhere in your template you can use this array:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM