Angular 错误 NG8002,错误 TS2341(html 和 ts 文件有问题)
Angular error NG8002, error TS2341 (Problem with the html and ts files)
我找了2个小时的错误。一切似乎都很好,并且在我添加 [(NgModule)]="toDo$.label" 之前它就可以工作。它还在 template-todo-form.component.ts 中声明为:
这是 html 文件 -> template-todo-form.component.html
<div class="todo-form">
<div class="todo-content">
<input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
</div>
<div class="create">
<span>Erstellen</span>
</div>
</div>
这是 Typescript 摘要:
import { Component, OnInit } from '@angular/core';
import { ToDo } from '../../_interface/todo';
@Component({
selector: 'app-template-todo-form',
templateUrl: './template-todo-form.component.html',
styleUrls: ['./template-todo-form.component.sass']
})
export class TemplateTodoFormComponent implements OnInit {
private toDo$: ToDo;
constructor() {
this.toDo$ = {
label: undefined,
status: false
};
}
ngOnInit() {
}
// Create new ToDo
}
为了您的兴趣 -> 我正在使用 gitbash,所以我复制并粘贴了错误输出。
$ ng serve
- Generating browser application bundles...
 Browser application bundle generation complete.
Initial Chunk Files | Names | Size
main.js | main | 0 bytes
polyfills.js | polyfills | 0 bytes
runtime.js | runtime | 0 bytes
styles.css, styles.js | styles | 0 bytes
vendor.js | vendor | 0 bytes
| Initial Total | 0 bytes
Build at: 2021-03-23T18:10:05.416Z - Hash: 5b02fccf2cebb89bf415 - Time: 8328ms
Error: _template/template-todo-form/template-todo-form.component.html:3:28 - error NG8002: Can't bind to 'NgModel' since it isn't a known property of 'input'.
3 <input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
~~~~~~~~~~~~~~~~~~~~~~~~~
_template/template-todo-form/template-todo-form.component.ts:6:16
6 templateUrl: './template-todo-form.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component TemplateTodoFormComponent.
Error: _template/template-todo-form/template-todo-form.component.html:3:41 - error TS2341: Property 'toDo$' is private and only accessible within class 'TemplateTodoFormComponent'.
3 <input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
~~~~~
_template/template-todo-form/template-todo-form.component.ts:6:16
6 templateUrl: './template-todo-form.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component TemplateTodoFormComponent.
Error: _template/template-todo-form/template-todo-form.component.html:3:41 - error TS2341: Property 'toDo$' is private and only accessible within class 'TemplateTodoFormComponent'.
3 <input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
~~~~~
_template/template-todo-form/template-todo-form.component.ts:6:16
6 templateUrl: './template-todo-form.component.html',
m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component TemplateTodoFormComponent.
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
I Hope the information is useful to help me asap...
Thanks at the front :-)
您需要使用的小写 'n'。
[(ngModel)]
绑定您绑定的值。所以你的 HTML 应该是这样的:
<input type="text" [(ngModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
我找了2个小时的错误。一切似乎都很好,并且在我添加 [(NgModule)]="toDo$.label" 之前它就可以工作。它还在 template-todo-form.component.ts 中声明为:
这是 html 文件 -> template-todo-form.component.html
<div class="todo-form">
<div class="todo-content">
<input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
</div>
<div class="create">
<span>Erstellen</span>
</div>
</div>
这是 Typescript 摘要:
import { Component, OnInit } from '@angular/core';
import { ToDo } from '../../_interface/todo';
@Component({
selector: 'app-template-todo-form',
templateUrl: './template-todo-form.component.html',
styleUrls: ['./template-todo-form.component.sass']
})
export class TemplateTodoFormComponent implements OnInit {
private toDo$: ToDo;
constructor() {
this.toDo$ = {
label: undefined,
status: false
};
}
ngOnInit() {
}
// Create new ToDo
}
为了您的兴趣 -> 我正在使用 gitbash,所以我复制并粘贴了错误输出。
$ ng serve
- Generating browser application bundles...
 Browser application bundle generation complete.
Initial Chunk Files | Names | Size
main.js | main | 0 bytes
polyfills.js | polyfills | 0 bytes
runtime.js | runtime | 0 bytes
styles.css, styles.js | styles | 0 bytes
vendor.js | vendor | 0 bytes
| Initial Total | 0 bytes
Build at: 2021-03-23T18:10:05.416Z - Hash: 5b02fccf2cebb89bf415 - Time: 8328ms
Error: _template/template-todo-form/template-todo-form.component.html:3:28 - error NG8002: Can't bind to 'NgModel' since it isn't a known property of 'input'.
3 <input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
~~~~~~~~~~~~~~~~~~~~~~~~~
_template/template-todo-form/template-todo-form.component.ts:6:16
6 templateUrl: './template-todo-form.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component TemplateTodoFormComponent.
Error: _template/template-todo-form/template-todo-form.component.html:3:41 - error TS2341: Property 'toDo$' is private and only accessible within class 'TemplateTodoFormComponent'.
3 <input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
~~~~~
_template/template-todo-form/template-todo-form.component.ts:6:16
6 templateUrl: './template-todo-form.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component TemplateTodoFormComponent.
Error: _template/template-todo-form/template-todo-form.component.html:3:41 - error TS2341: Property 'toDo$' is private and only accessible within class 'TemplateTodoFormComponent'.
3 <input type="text" [(NgModel)]="toDo$.label" placeholder="Was muss erledigt werden?">
~~~~~
_template/template-todo-form/template-todo-form.component.ts:6:16
6 templateUrl: './template-todo-form.component.html',
m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component TemplateTodoFormComponent.
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
I Hope the information is useful to help me asap...
Thanks at the front :-)
您需要使用的小写 'n'。
[(ngModel)]
绑定您绑定的值。所以你的 HTML 应该是这样的:
<input type="text" [(ngModel)]="toDo$.label" placeholder="Was muss erledigt werden?">