I'm stuck in ionic Storage. Need help

Hi

Let me explain to you. What I’ve done and what is remaining.

Done:
Checkbox value is saved in storage and it is successfully working.

Remaining:
On clicking checkbox, I’m adding a class to content but I don’t know how to save the class to storage.

Popover.html

 <ion-item>
    <ion-label>English</ion-label>
    <ion-checkbox (click)="activateEnglish()" [checked]="EnglishCheckbox"></ion-checkbox>
  </ion-item>

Popover.ts

constructor(public navCtrl: NavController, public navParams: NavParams, public viewCtrl: ViewController, private storage: Storage) {
    storage.get('EnglishLang').then(EnglishLang=> this.EnglishCheckbox = EnglishLang);
  }

EnglishCheckbox: boolean = false;

  //Active English Function
  activateEnglish(){
    this.textEle.classList.toggle('showEnglish');
    this.storage.set('name', 'test').then(() => {
      if (this.EnglishCheckbox){
        this.EnglishCheckbox = false;
        this.storage.set('EnglishLang', 'false');
      } else {
        this.EnglishCheckbox = true;
        this.storage.set('EnglishLang', 'true');
      }
    });
  }

On click checkbox I’m adding showEnglish class to my content but it is not saving it to storage. How to do it.

Best Regards

Anyone there to reply?

use (ionChange) instead of (click)

...
(ionChange)="activateEnglish()"
...

How to save it into storage?

In your TS file

activatEnglisg() {
   this.storage.set('yourKey', 'yourValue').then(() => {
        console.log('success');
    });
}

activateEnglish(){
    //Add Class to content
    this.textEle.classList.toggle('showEnglish');

    //Save Checkbox Value
    this.storage.set('name', 'test').then(() =&gt; {
      if (this.EnglishCheckbox){
        this.EnglishCheckbox = false;
        this.storage.set('EnglishLang', 'false');
      } else {
        this.EnglishCheckbox = true;
        this.storage.set('EnglishLang', 'true');
      }
    });
  }

this.textEle.classList.toggle(‘showEnglish’);

I want to save it into the storage then how I’ll do it?

what does this ? it’s a function ?

It is adding a “showEnglish” class to my content in another page.

So you want save this css class ‘showEnglish’ in your storage ?
that’s right ?

Yes, that’s very right.

save it in storage

this.storage.set('showEnglishKey', 'showEnglish').then(() => {

});

to get it after saving

this.storage.get('showEnglishKey').then((resp) => {
   this.variable = resp; // will contains showEnglish
});

Thanks man! Let me try this.

What variable will be here?

I haven’t declared any.

chose any one to store your value from storage
for example declare a variable in your class in TS file.

It is saving the value to storage but class not adding to content.

I’m doing it like this.

saveClass: any;

activateEnglish(){
    this.storage.set('showEnglishKey', 'showEnglish').then(() => {
      this.textEle.classList.toggle('showEnglish');
});

and getting it like this:

this.storage.get('showEnglishKey').then((resp) => {
      this.saveClass = resp; // will contains showEnglish
});

try this

this.storage.get('showEnglishKey').then((resp) => {
     this.saveClass = resp; // will contains showEnglish
     this.textEle.classList.toggle( this.saveClass );
});
1 Like

Thanks man! It is working now. Thanks a lot…
:blush:

you are welcome :wink: