[英]Fetching data from storage doesn't refresh page content
在我的 Ionic 2 應用程序中,我從 SqlStorage 和 HTTP.get 請求中獲取數據。 當我從 HTTP 請求獲取數據時,我的頁面會使用新數據按預期刷新,但是當我從 SqlStorage 獲取數據時,它不會刷新,並且新數據僅在我“拉動刷新”時顯示。
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 更改檢測,您可以使用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 區域以運行更改檢測。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.