# \[SOLVED\]Ion-checkbox dynamically set checked

**URL:** <https://forum.ionicframework.com/t/solved-ion-checkbox-dynamically-set-checked/95107>\
**Category:** ionic-v3\
**Created:** [June 20, 2017, 11:44am UTC](https://forum.ionicframework.com/t/solved-ion-checkbox-dynamically-set-checked/95107 "2017-06-20T11:44:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaurav\_ch](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gaurav_ch/32/92509_2.png) [@gaurav\_ch](https://forum.ionicframework.com/u/gaurav_ch)\
**Post date:** [June 20, 2017, 11:44am UTC](https://forum.ionicframework.com/t/solved-ion-checkbox-dynamically-set-checked/95107/1 "2017-06-20T11:44:55Z")

</div>

Here is the html code:

```auto
<ion-checkbox color="dark" [checked]="selectedCategory.indexOf(category.i_id) >= 0" (ionChange)="updateCheckedOptions(category.i_id,$event)"></ion-checkbox>

```

Here is the ionViewDidLoad() method:

```auto
for (let i = 0; i < vendor.category_id.length; i++) {
            this.selectedCategory.push(
                  vendor.category_id[i].i_vendor_category_id
                );
            }

```

and the ionChange event method:

```auto
updateCheckedOptions(id) {
    let idx = this.selectedCategory.indexOf(id);
    if (idx > -1) {
      this.selectedCategory.splice(idx, 1);
    } else {
      this.selectedCategory.push(id);
    }
    console.dir(this.selectedCategory);
  }

```

Problem: When I am dynamically setting the `selectedCategory` array with the values, I get an error:

```auto
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.

```

If I comment the `updateCheckedOptions` method, I don’t get the error but I am not able to perform my task on ionChange. I need this method to set the array when the user taps on the checkboxes.

How to keep the ionChange method working and not get the above error? Can anybody help?

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [June 20, 2017, 12:01pm UTC](https://forum.ionicframework.com/t/solved-ion-checkbox-dynamically-set-checked/95107/2 "2017-06-20T12:01:57Z")

</div>

I’ve found it a good policy to bind always to variable names. `[property] = nameOfVariable` Then I change the value of `nameOfVariable` in the ts file. This isn’t necessary, but I’m pretty sure it’s helped me avoid a lot of weird errors. If you do that, you’ll probably get a page that compiles but behaves incorrectly. Then you can improve the page’s behavior.

---

<div class="post-metadata">

**Author:** ![gaurav\_ch](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gaurav_ch/32/92509_2.png) [@gaurav\_ch](https://forum.ionicframework.com/u/gaurav_ch)\
**Post date:** [June 20, 2017, 12:07pm UTC](https://forum.ionicframework.com/t/solved-ion-checkbox-dynamically-set-checked/95107/3 "2017-06-20T12:07:37Z")

</div>

changed (ionChange) to (click). it worked. Not marking this as solved as I have to test on the device. Hopefully this will work.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [June 20, 2017, 9:56pm UTC](https://forum.ionicframework.com/t/solved-ion-checkbox-dynamically-set-checked/95107/4 "2017-06-20T21:56:25Z")

</div>

I completely agree with @AaronSterling here that overly cute template expressions and change handlers not only introduce lots of hard-to-understand bugs, but also result in poor performance because of how many times they are executed. I would simply bind to an `isSelected` property of each `category` here.
