简体   繁体   English

复选框没有返回false angular 2

[英]Checkbox is not returning 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 我在allBookData中有3本书

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

So now I want Mr Romeo checkbox is checked since the book id == 3 , but all checkboxes are checked. 所以现在我想要检查罗密欧先生复选框,因为书ID == 3 ,但所有复选框都被选中。 I think my condition in checked="" is wrong. 我认为我在check =“”中的条件是错误的。 What is the correct condition I should do there. 我应该在那里做的正确条件是什么。

I have also tried this 我也尝试过这个

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

still not working. 还是行不通。

try [checked]="expression" 试试[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>

BTW, the value property is not recommened here, better to use [(ngModel)] . 顺便说一下,这里不推荐使用value属性,最好使用[(ngModel)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM