从存储中获取数据不会刷新页面内容

Fetching data from storage doesn't refresh page content

在我的 Ionic 2 应用程序中,我从 SqlStorage 和 HTTP.get 请求中获取数据。当我从 HTTP 请求中获取数据时,我的页面使用新数据按预期刷新,但是当我从 SqlStorage 获取数据时,它不会刷新,并且仅在 'pull to refresh'.[= 时显示新数据13=]

PS:我正在使用 TypeScript。

这是 SqlStorage 获取:

 oldFeeds() {
  return new Promise((resolve, reject) => {
    this.storage.get('feeds').then(data => {
      resolve(JSON.parse(data));
    });
  });
}

这是 HTTP 获取:

feeds() {
  return new Promise((resolve, reject) => {
    this.http.get('http://foo.bar').map(res => res.json()).subscribe(data => {
      resolve(data);
    },
    e => reject(e));
  });
}

这些方法在一个服务中,对它们的调用完全相同:

this.feedsService.oldFeeds().then(data => {
  this.feeds = data;
  this.entries = this.feeds[this.activeIndex].entries;
});

this.feedsService.feeds().then(data => {
  this.feeds = data;
  this.entries = this.feeds[this.activeIndex].entries;
});

关于为什么会发生这种情况以及我能做些什么来解决它的任何想法? 谢谢!

更新

要推动 Angulars 变化检测 "manually" 你可以使用 ApplicationRef

import {ApplicationRef} from 'angular2/core';

constructor(private appRef:ApplicationRef) {}

oldFeeds() {
  return new Promise((resolve, reject) => {
    this.storage.get('feeds').then(data => {
      resolve(JSON.parse(data));
      this.appRef.tick();
    });
  });
}

原创

无效 - 查看评论

constructor(private zone:NgZone) {}

oldFeeds() {
  return new Promise((resolve, reject) => {
    this.storage.get('feeds').then(data => {
      this.zone.run(() => {
        resolve(JSON.parse(data));
      });
    });
  });
}

看起来 SqlStorage API 没有被 Angulars 区域修补,需要将调用带回 Angulars 区域 "manually" 以检测 运行.

另见