Checkbox does not return false angular 2

<div *ngFor="let bookData of allBookData.books">
      <input #bookArray type="checkbox" id="bookArray " name="bookArray " class="form-control" value="bookData.bookname" checked="bookData.book_id == 3"> {{bookData.bookname}}
</div>

I have 3 books in allBookData

book_id :1 bookname: The Magician
book_id :2 bookname: Rainbow Behind Tree
book_id :3 bookname: Mr Romeo

So now I want the Mr Romeo flag to be checked with book id == 3 , but all the flags are checked . I think my condition in checked = "" is wrong. What is the right condition I have to make there.

I tried this too

checked ="bookData.book_id == 3 ? true : false"

still not working.

+4
source share
1 answer

try [checked]="expression"

<div *ngFor="let bookData of allBookData.books">
  <input #bookArray type="checkbox" id="bookArray " name="bookArray " class="form-control" value="bookData.bookname" [checked]="bookData.book_id === 3"> {{bookData.bookname}}
</div>

By the way, the property valueis not recommended here, it is better to use [(ngModel)].

+4
source

Source: https://habr.com/ru/post/1674397/


All Articles