首页 > 解决方案 > 错误 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>

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

标签: htmlangulartypescript

解决方案


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

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

推荐阅读