Skip to content
loading...

Angular Checkbox doesn't check

jwp profile image John Peters github logo ・1 min read

Try this binding first

   <input 
      #checkbox
      (change)="onCheckBoxChanged(checkbox, setting)"
      [checked]="setting.values[0] != 'false'"       
      type="checkbox"
   />

Where the variable setting is a part of a parent *ngFor loop. This is different than other [(ngModel)] bindings; in that, we are going directly to the input checked attribute. What's interesting is that we had to add the comparator to get it to work.


[checked]="setting.values[0] != 'false'"  

Even though setting.values[0] only had string values of "true" and "false" binding directly to it like these two statements:

 [checked]="setting.values[0]"
 [(ngModel)]="setting.values[0]

Did not work! Not sure why, but I do know it can chew up time finding these syntactic errors.

JWP2020

twitter logo DISCUSS
Discussion
markdown guide
Classic DEV Post from Aug 4 '19

You're not worth hiring unless...

John Peters profile image
Angular, React, Typescript and JavaScript. The wave of now!