javascript - ionic 2 在 View 加载之前渲染数据

标签 javascript jquery angular ionic2 ionic-view

我正在学习 ionic 2,目前正面临一些 ionic 生命周期问题。 我想在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);

    });

  }

}

我尝试使用 constructor() 调用 api,也尝试使用 **ngOnInit() 和 ionViewDidLoad() ** 数据正常但没有反射(reflect)在我的 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>

因此,当我单击类别选项卡时,api 正在调用并获得响应但无法在我的 View 页面上看到此结果,现在如果我单击任何其他选项卡并再次单击类别选项卡,那么我可以看到此数据.

因为我是 ionic 2 的新手,所以我无法解决这个问题。 谁能帮我解决这个问题。

提前致谢:)

最佳答案

  1. 最好从服务类或提供者类中触发 HTTP 请求
  2. IonViewDidLoad仅在加载页面时运行,因此将在触发 HTTP 请求之前加载空页面。
  3. 使用IonViewCanEnter可以在 View 呈现之前运行
  4. 使用 ionic loading component 显示加载这样用户就会知道后台发生了什么

关于javascript - ionic 2 在 View 加载之前渲染数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47236620/

相关文章:

javascript - 谷歌地图未捕获类型错误: Cannot read property 'start_location' of undefined in google maps

javascript - 无法使用 jquery $.ajax 访问从 php 页面检索的 json 数据

javascript - JSON 和单选按钮输入?

angular - 如何在管道内调用 forkJoin?

javascript - 缩小每个目录的 JS 和 CSS

用于滚动效果的 Javascript 代码适用于 Safari/chrome,不适用于 firefox/opera

javascript - 我怎样才能解决这个视差

javascript - 在 jquery each() 迭代器函数中访问 JS 对象

Angular/RxJs - 使用 take(1) 取消订阅依赖的 http 服务?

angular - ngrx 8 : Access state of @ngrx/data entity from reducer function