angular - angular6: passing value of the input field to the component
问题描述
I am trying to pass value of the input field to the component class. But it does not seem to be working. Please find the code below:
todoinput.component.html
<mat-card>
<form>
<mat-form-field class="example-full-width">
<input matInput type="text" name="todo" placeholder="Enter a todo">
</mat-form-field>
<button mat-raised-button color="primary" type="button" (click)="addTodo(todo.value)">Add Todo
</button>
</form>
</mat-card>
todoinput.component.ts
addTodo(text) {
this._todosService.addTodo({
text: text,
}).subscribe((todo) => {
return this.newTodo.emit(todo);
})
}
But clicking on the button throws the error below:
ERROR TypeError: Cannot read property 'value' of undefined
at Object.eval [as handleEvent] (TodoinputComponent.html:7)
at handleEvent (core.js:10258)
I am using angular material framework for rendering the elements. Could anyone let me know what am I doing wrong here ?
Thanks
解决方案
尝试添加#todo
到输入。之后,角度应该能够访问正确的输入。没有它,它会得到未定义的变量。
<mat-card>
<form>
<mat-form-field class="example-full-width">
<input #todo matInput type="text" name="todo" placeholder="Enter a todo">
</mat-form-field>
<button mat-raised-button color="primary" type="button" (click)="addTodo(todo.value)">Add Todo
</button>
</form>
</mat-card>
推荐阅读
- c++ - C++ 控制台应用程序调用 C++/CLI 静态库
- elasticsearch - 在搜索唯一 ID 时,它给出了多个计数
- amazon-web-services - 通过 AWS CLI 中的 aws ssm 启动会话记录后传递命令
- jquery - Jquery 音频位置控制
- linux - 用于查找机器人元标记值的 Bash shell 脚本
- c++ - 在 C++ 中使用 istringstream 读取输入时出错
- mysql - 按特定条件订购商品,然后是其余商品
- ios - 线程 1:EXC_BAD_ACCESS(代码=2,地址=0x16d0f3ff0)
- latex - 使用 \bibentry 显示内联引文前面的数字
- ios - 在iOS(Swift)中生成多个环境后如何处理问题?