2017-03-24 3 views
0

私はionic 2クラスを持っている:にイオン2のモデルの変化を聞く方法がありますか?

export class SettingsPage { 

    public num = 2; 
    public num1 = 3; 

    constructor(public navCtrl: NavController, public navParams: NavParams, 
       public storage: Storage) { 
    storage.ready().then(() => { 

     // Or to get a key/value pair 
     storage.get('num').then((val) => { 
     if (val != null) { 
      this.num = val; 
     } else { 
      this.num = 2; 
     } 

     }) 
    }); 
    } 

のベストプラクティスは何か?変数(numnum1)のいずれかの変更を聴いて、その値をストレージに戻して永続化する方法はありますか?

答えて

3

この質問はIonicとは関係ありませんが、TypeScriptと関連しています。

あなたのようget & set方法でプロパティを定義することができます

class YouClass { 
    private _num: boolean = false; 

    get num(): boolean { 
     return this._num; 
    } 

    set num(value: boolean) { 
     this._num = value; 

     // Here you can save the value in the storage ... 
     this.storage.set('num', value); 
    } 
} 

そしてsetに、あなたはあなたが欲しい、これまで何を行うことができます。ストレージへの保存を含む。

関連する問題