javascript - Angular: Async get data from Backend -
well, problem this. have backend provides me database products in paged form (50 per request). in same request, gives me url of next page , previous one. type this:
{ "count": 6348, "next": "http: // localhost: 8000 / products /? page = 2", "previous": null, "products": [...] } i'm doing in angular 2 frontend , needed kind of loop fetch 50 products @ time , append them array. intended asynchronous user can navigate page first 50 loaded.
controller:
// products this._http.getproducts().subscribe((db_products) => { this.products = db_products; }); service
getproducts( page = 1, products = [] ) { return this.http.get(this._api_url + `/products/?page=${page}`, this.options) .catch(this.handleerror) .map((res: any) => { const body = res.json(); products = products.concat(body.data); if ( body.next !== null) { // next page available -> recursion return this.getproducts(page + 1, products); } // return products return products; }); }
Comments
Post a Comment