angular6:将输入字段的值传递给组件

Ash*_*csi 4 parameter-passing angular angular6

我试图将输入字段的值传递给组件类。但是它似乎没有用。请在下面找到代码:

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>
    &nbsp; &nbsp;
    <button mat-raised-button color="primary" type="button" (click)="addTodo(todo.value)">Add Todo
    </button>
  </form>
</mat-card>
Run Code Online (Sandbox Code Playgroud)

todoinput.component.ts

  addTodo(text) {
    this._todosService.addTodo({
      text: text,
    }).subscribe((todo) => {
      return this.newTodo.emit(todo);
    })
  }
Run Code Online (Sandbox Code Playgroud)

但是单击按钮会引发以下错误:

ERROR TypeError: Cannot read property 'value' of undefined
    at Object.eval [as handleEvent] (TodoinputComponent.html:7)
    at handleEvent (core.js:10258)
Run Code Online (Sandbox Code Playgroud)

我正在使用角度材质框架渲染元素。有人可以让我知道我在做什么错吗?

谢谢

Art*_*pov 6

尝试添加#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>
    &nbsp; &nbsp;
    <button mat-raised-button color="primary" type="button" (click)="addTodo(todo.value)">Add Todo
    </button>
  </form>
</mat-card>
Run Code Online (Sandbox Code Playgroud)