新来离子和棱角,我真的很喜欢它(我终于看到了Java中的JavaScript!),但我有一个检测数据发生变化后的问题。 我有一个使用离子存储模块保存/检索模拟数据的服务,但是当我使用该服务在我的页面构造函数中检索数据时,我的HTML不会更改。Ionic2存储模块/正在更新列表
import { Component, Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class IconService{
private allNotifications:Notification[] = [];
constructor(private storage:Storage) {
storage.ready().then(() => {});
}
public getAllNotifications(){
this.storage.get('allNotifications').then((data)=>{
this.allNotifications = JSON.parse(data);
});
return Observable.create(observer => {
observer.next(this.allNotifications);
observer.complete();
});
}
}
当我订阅我的页面可观察到的,我的列表将不会更新,直到我有一个按钮交互。我希望列表在页面打开后立即更新。
@Component({
selector: 'page-home',
templateUrl: 'home.html',
})
export class HomePage {
constructor(public navCtrl: NavController, public navParams:NavParams, public modalCtrl:ModalController,
private iconService:IconService,private storage:Storage) {
this.iconService.getAllNotifications().subscribe(data =>this.notifications = data);
}
ionViewWillEnter(){
this.iconService.getAllNotifications().subscribe(data =>this.notifications = data);
}
}
然而,当我打电话storage.get()
从我的页面
constructor(public navCtrl: NavController, public navParams:NavParams, public modalCtrl:ModalController,
private iconService:IconService,private storage:Storage) {
this.storage.get('allNotifications').then((data)=>{
this.notifications = JSON.parse(data);
});
}
的数据加载和HTML是只要打开页面更新。这是如果需要的HTML:
<div *ngIf='notifications'>
<button ion-fab *ngFor='let notification of notifications' [color]="day ? 'light' : 'moon'"><ion-icon name='{{notification.icon}}'></ion-icon></button>
</div>
我宁愿用我的服务,然后直接与存储虽然互动,所以我究竟做错了什么?我也尝试不返回一个observable,只是简单地返回解析的JSON数据,但这也不起作用。
离子/角大师,请帮忙!
这个工作,非常丰富的为好,谢谢! – afryingpan
很高兴帮助:) – sebaferreras