如何获取组件 class 中的 ngForm 变量引用?

How to get ngForm variable reference in the component class?

鉴于以下...

.html

<form (ngSubmit) = "onSubmit()"
          #heroForm = "ngForm">
      {{diagnostic}}
      <div class = "form-group">
        <label for = "name">Name</label>
        <input type = "text"
               class = "form-control"
               required
               [(ngModel)] = "model.name"
               ngControl = "name"
               #name = "ngForm"
               #spy>
        <p *ngIf = "name.dirty"
           class = "alert alert-danger">
          Name is required
        </p>
        <!--<p [hidden] = "name.dirty"-->
           <!--class = "alert alert-danger">-->
          <!--Name is required-->
        <!--</p>-->
      </div>

..

..是否可以在 .dart 组件中获取 #name = "ngForm" (ngForm) 引用以允许操作?欢迎任何建议和指正。

导入这个 -

import {ViewChild} from 'angular2/core';

只需将带有注释的此字段添加到 class

// Dart syntax
@ViewChild('heroForm') NgForm heroForm;

你不能在构造函数中使用它,因为它只是稍后设置的。在 ngAfterViewInit 或用户输入的事件处理程序中,您可以不受限制地使用它。