错误 TS2322:类型“事件”不可分配给类型“布尔值”

新手上路,请多包涵

我正在写一个 todolist 演示。当 ng serve 它时,它显示错误:

 Error: src/app/app.component.html:17:58 - error TS2322: Type 'Event' is not assignable to type 'boolean'.
17  <input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" >
                                                       ~~~~~~~~~~~~~~
18
19  <label>{{ todo.title }}</label>
   ~~

也没有检查所有项目。(即使他们的 isDone 状态是真的)

我在 app.component.ts 中定义了一个对象。

 public todos:{
    id:number,
    title: string,
    isDone: boolean
  }[]=todos

const todos=[
  {
  id:1,
  title:'study',
  isDone:true
},{
  id:2,
  title:'sleep',
  isDone:true
},{
  id:3,
  title:'drink',
  isDone:true
}
]

app.component.html 如下。

 <li *ngFor="let todo of todos">
                <div class="view">
                    <input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" >
                    <label>{{ todo.title }}</label>
                    <button class="destroy"></button>
                </div>
                <input class="edit" value="Create a TodoMVC template">
            </li>

谁能看到我做错了什么?谢谢!

原文由 RainOnMe77 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 767
2 个回答

将您的代码从 <input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" > 更改为…

 <input class="toggle" type="checkbox" [(ngModel)]="todo.isDone" >

在这里,ngModule 没有识别..

原文由 Developer 发布,翻译遵循 CC BY-SA 4.0 许可协议

当我收到此错误时,是因为我忘记在 FormsModule 中导入 app.module.ts 。所以在 app.module.ts 中:

 import { FormsModule } from '@angular/forms';
...
imports: [
..,
FormsModule
],

原文由 anosha_rehan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题