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

Rai*_*e77 6 html typescript angular

我正在写一个 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>
   ~~
Run Code Online (Sandbox Code Playgroud)

也不会检查所有项目。(即使它们的 isDone 状态为真)

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

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>
   ~~
Run Code Online (Sandbox Code Playgroud)

app.component.html 如下。

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
}
]
Run Code Online (Sandbox Code Playgroud)

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

小智 14

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

import { FormsModule } from '@angular/forms';
...
imports: [
..,
FormsModule
],
Run Code Online (Sandbox Code Playgroud)


Gau*_*rda 3

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

<input class="toggle" type="checkbox" [(ngModel)]="todo.isDone" >
Run Code Online (Sandbox Code Playgroud)

在这里,ngModule 无法识别..