I have an ionic 2 class:
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;
}
})
});
}
what is the best practice? is there a way to listen to any change in any of the variables (num, num1) and then set the value back to storage to persist it?
The question is not related to Ionic but TypeScript.
You can define property with
get&setmethods like:And in
setyou can do what ever you want. Including saving to storage.