1

I'm using ionic with angular, when i'm creating alert in ok button's callback function i'm changing my state. State changes, but this change doesn't effects in iu, i think component is not updating. How can I fix this?

async presentAlert() {
    const alert = await this.alertController.create({
      header: '',
      message: '',
      buttons: [
        'cancel',
        {
          text: 'ok',
          handler: () => {
            this.currentScreen = "";
            this.dates[this.currentDateIndex].isOrdered = false;//disable order
          }
        }
      ]
    });
    await alert.present();
  }
2
  • 1
    Can you provide the full component code? It's a little hard to tell what's going on here without understanding the full context - namely, how is your component configured via its decorator, what calls presentAlert(), what the alertController is/does, etc etc. Commented Feb 10, 2020 at 18:01
  • I don't see a state change in there, is this the function that updates state? Commented Feb 10, 2020 at 19:27

1 Answer 1

4

You can try to use ChangeDetectorRef to explicitly state that change has been made and view needs to be updated.

Reference: https://angular.io/api/core/ChangeDetectorRef

Example:

Declare ChangeDetectorRef in constructor

constructor(public cd: ChangeDetectorRef) {}

Then use it in your callback:

buttons: [
    'cancel',
    {
      text: 'ok',
      handler: () => {
        this.currentScreen = "";
        this.dates[this.currentDateIndex].isOrdered = false;//disable order
        this.cd.detectChanges();
      }
    }
  ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.