javascript - 异步调用返回后重新加载页面内容

标签 javascript node.js angular ionic-framework ionic2

我有 Ionic2 应用程序和 SideMenu 模板,在 rootPage 上我有这段代码

export class HomePage {

  products: any = [];

  constructor(public navCtrl: NavController, public navParams: NavParams, private woo: WooCommerce) {

  }

  ionViewDidLoad() {
    this.woo.Fetch('products', function (res) {
      this.products = res.products;
      //console.log(this.products[0]);
    }.bind(this));
  }

  ngOnInit() {
  }

}

其中 WooCommerceWooCommerce-Nodejs 上的提供者包装器

export class WooCommerce {

    woo: any = null;

    constructor(public http: Http, private util: Utilities) {
        this.woo = WooCommerceAPI();
    }

    Fetch(what, callback) {
        return this.woo.getAsync(what).then(function (result) {
            callback(JSON.parse(result.body));
        });
    }
}

在我的 page.ts

   ionViewDidLoad() {
        this.woo.Fetch('products', function (res) {
          this.products = res.products;
          console.log(this.products);
        }.bind(this));
      }

page.html

 <ion-col center text-center col-6 *ngFor="let product of products">
        <ion-card>
          <img src="{{product.featured_src}}" />
          <ion-card-content>
            <ion-card-title style="font-size: 100%">
              {{ product.title | StripHTML }}
            </ion-card-title>
          </ion-card-content>
          <ion-row center text-center>
              <p style="color: red">
                {{ product.price_html | StripHTML:{split:" ",index:0} }}
              </p>
            </ion-row>
        </ion-card>
      </ion-col>

问题:Fetch 加载并返回数据,但页面 View 没有刷新,直到我单击菜单切换按钮,然后页面重新呈现或刷新并且产品/数据显示...

Fetch 调用它重新渲染或刷新的 callback 函数时,是否可以做到这一点?

最佳答案

Angular 通常会检测变化并更新其 View 。它可能没有在 Woocommerce API 中获得更新。 尝试使用 ngZone以确保 Angular 检测到更改。

import {NgZone} from '@angular/core'
constructor(ngZone: NgZone){}//inject in constructor

   ionViewDidLoad() {
        this.woo.Fetch('products', function (res) {
          this.ngZone.run(()=>{//use here
              this.products = res.products;
              console.log(this.products);
            });
        }.bind(this));
      }

关于javascript - 异步调用返回后重新加载页面内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228534/

相关文章:

javascript - AngularJS 输入更改重置

node.js - 将 jade 变量分配给 Angular

angular - DatePicker 在 Mat Dialog 窗口中不工作

javascript - 条件语句中的Node.js调度函数

angular - 包装或扩展 Angular Material 组件,允许将未知属性传递给子组件

javascript - CORS 奇怪的行为

javascript - iOS 上的 JQuery Mouseup

javascript - 如何处理超出范围的值? switch 还是 if 语句?

javascript - 如何限制玩家在 socket.io 游戏中的回合时间?

javascript - 从生成的进程访问 Sails Controller