Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5, HTML, boolean on checkbox is checked

Angular 5, Typescript 2.7.1

I can't seem to get the checkbox to be checked when returning a boolean, I've tried, item.check returns either true or false.

<tr class="even" *ngFor="let item of rows"> <input value="{{item.check}}" type="checkbox" checked="item.check"> 

The checkbox is always checked when checked is written inside input. And it does not get unchecked when checked="false".

Is there a better way to do it with Angular features instead? like ngModel or ngIf???

Solution

<input type="checkbox" [checked]="item.check == 'true'"> 
like image 292
Mr. Toast Avatar asked Feb 28 '18 13:02

Mr. Toast


2 Answers

try:

[checked]="item.checked" 

check out: How to Deal with Different Form Controls in Angular

like image 195
ProfitWarning Avatar answered Sep 28 '22 05:09

ProfitWarning


You can use this:

<input type="checkbox" [checked]="record.status" (change)="changeStatus(record.id,$event)"> 

Here, record is the model for current row and status is boolean value.

like image 33
Abdus Salam Azad Avatar answered Sep 28 '22 04:09

Abdus Salam Azad