简体   繁体   English

为什么切换复选框返回 false?

[英]Why toggle checkbox returns false?

I show checkboxes on the page.我在页面上显示复选框。

When I click over it it is changed to true/false .当我点击它时,它被更改为true/false If true the cehckbox should be checked.如果为真,则应检查 cehckbox。

Problem is when I click first checkbox it works, when click the second I get wrong completed: false in object {id: 2, content: "CSS", completed: false} , despite checkbox is checked:问题是当我点击第一个复选框时它起作用,当我点击第二个时我出错了completed: false in object {id: 2, content: "CSS", completed: false} ,尽管复选框被选中:

pic 1图 1

Code is by link to stackblitz代码是通过链接到stackblitz

You are using ngModel directive wrong its [(ngModel)] not ([ngModel])您使用的 ngModel 指令错了它的 [(ngModel)] 而不是 ([ngModel])

Checkboxes has rather a state (checked or not), not a value.复选框有一个 state(选中或不选中),而不是一个值。 If you are changing the model value after click, use checkbox only for showing the curent state. So i would change:如果您在单击后更改 model 值,请仅使用复选框显示当前 state。所以我会更改:

[(ngModel)]="todo.completed"

to [checked]="todo.completed"[checked]="todo.completed"

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

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