VRK*_*VRK 5 javascript jquery ionic-view ionic2 angular
我正在学习ionic 2,目前面临一些离子生命周期问题。我想在api获取成功数据后显示数据。
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {LoadingController} from 'ionic-angular';
import WooCommerceAPI from 'woocommerce-api';
@Component({
selector: 'page-category',
templateUrl: 'category.html',
})
export class CategoryPage {
information: any[] = [];
infoChild: any[] = [];
wooApi: any;
wooCat: any;
categories: any = [];
constructor(public navCtrl: NavController, public loadingCtrl: LoadingController) {
this.wooApi = WooCommerceAPI(
{
url: 'abc.com/api/v1/cat',
}
);
//here also tried to call api
}
ngOnInit() {
//here also tried to call api
}
ionViewDidLoad() {
console.log("calling api");
/*from with nginit*/
this.wooApi.getAsync('products/categories?parent=0').then((data) => {
console.log("success");
this.wooCat = JSON.parse(data.body);
//this.information = this.wooCat;
for (let i = 0; i < this.wooCat.length; i++) {
console.log("in for loop");
this.wooApi.getAsync('products/categories?parent=' + this.wooCat[i].id).then((data) => {
console.log("get success", i);
let wooChild = JSON.parse(data.body);
for (let x = 0; x < wooChild.length; x++) {
this.infoChild.push(wooChild[x]['name']);
}
//console.log(this.infoChild)
this.information.push({
'items': {
'name': this.wooCat[i]['name'],
'children': [{
'name': this.infoChild
}]
}
});
this.infoChild = [];
});
}
console.log("current data", this.information);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我试图用** ngOnInit()和ionViewDidLoad()的构造函数()来调用api 数据正在正确获取,但未反映在我的view / html页面上。
我的HTML代码如下:
<accordion *ngFor="let catParent of information" [title]="catParent.items.name">
<!-- (click)="itemSelected(item)" -->
<ion-list *ngFor="let catChild of catParent.items.children">
<button ion-item *ngFor="let catChildName of catChild.name">
{{ catChildName }}
</button>
</ion-list>
</accordion>
Run Code Online (Sandbox Code Playgroud)
因此,当我单击“类别”选项卡api正在调用并获得响应,但无法在查看页面上看到此结果时,现在,如果我单击任何其他选项卡,然后再次单击“类别”选项卡,则可以看到此数据。
由于我是ionic 2的新手,因此无法解决此问题。谁能帮我解决这个问题。
提前致谢 :)
| 归档时间: |
|
| 查看次数: |
7267 次 |
| 最近记录: |