[英]error TS2322: Type 'Event' is not assignable to type 'boolean'
I'm writing a todolist demo.我正在写一个 todolist 演示。 When
ng serve
it, it shows an error:当
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>
~~
Also all items not be checked.(even their isDone status is true)也没有检查所有项目。(即使他们的 isDone 状态是真的)
I def an object in app.component.ts.我在 app.component.ts 中定义了一个 object。
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 as below. 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>
Can anyone see what I'm doing wrong?谁能看到我做错了什么? thanks!
谢谢!
When I got this error, it was because I forgot to import the FormsModule
in app.module.ts
.当我收到此错误时,是因为我忘记在
FormsModule
中导入app.module.ts
。 So in app.module.ts:所以在 app.module.ts 中:
import { FormsModule } from '@angular/forms';
...
imports: [
..,
FormsModule
],
change you code from <input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" >
to this...将您的代码从
<input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" >
更改为...
<input class="toggle" type="checkbox" [(ngModel)]="todo.isDone" >
Here, ngModule is not recognizing..在这里,ngModule 无法识别..
In my case it was because I had made my own component and it didn't have the output event emitter (visibleChange) defined.就我而言,这是因为我制作了自己的组件,并且没有定义 output 事件发射器 (visibleChange)。
Code sample with emitter highlighted:突出显示发射器的代码示例:
export class DialogComponent {
@Input() header = '';
@Input() message = '';
@Input()
get visible(): boolean { return this._visible; }
set visible(value: boolean) {
this._visible = value;
}
private _visible = false;
@Output() visibleChange = new EventEmitter<boolean>();
@Output() closeDialog = new EventEmitter<void>();
onCloseDialog(): void {
this.closeDialog.emit();
}
} }
This error can also happen in the following situation:在以下情况下也可能发生此错误:
The fix is to export the component from its declaring module.解决方法是从其声明模块中导出组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.