【发布时间】:2018-12-14 08:59:10
【问题描述】:
我是 Angular 新手,正在尝试创建一个简单的待办事项应用程序。
我使用 [(ngModel)] 将输入值传递给组件,但似乎我的做法不正确。
这是我的代码
HTML:
<div class="todo-app">
<h1>Todo List</h1>
<div>
<input type="text" class="input" placeholder="Enter your task" autofocus="" [(ngModel)]="value">
<button class="btn" (click)="addTodo()">Add</button>
</div>
</div>
ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.styl']
})
export class AppComponent {
todos = [];
id:number = 0;
addTodo() {
this.todos = [...this.todos, {title: this.value, id: this.id++, completed: false}];
return this.todos;
}
removeTodo(id:number) {
return this.todos.filter(todo => todo.id !== id)
}
toggleCompletionState(id:number) {
this.todos.map(todo => todo.id === id ? {...todo, completed: !todo.completed} : todo)
}
getAllTodos() {
return this.todos;
}
}
这里出了什么问题以及为什么 ngModel 不起作用??
【问题讨论】:
-
您忘记声明您的
value字段。不过,这应该可以工作(但是,当您拥有多个值时,将单个值存储为属性有点奇怪)。无论如何,请详细说明您的问题是什么。 -
我很困惑,我什至不知道如何获取输入值并将其传递给组件以使用它,你能告诉我一个演示如何实现吗??
标签: angular