鉴于以下内容......
<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)引用以允许操作?欢迎提出任何建议和更正。
答案 0 :(得分:10)
导入此 -
import {ViewChild} from 'angular2/core';
只需将带有注释的字段添加到类
即可@ViewChild('heroForm') NgForm heroForm;
你不能在构造函数中使用它,因为它只是稍后设置的。在用于用户输入的ngAfterViewInit
或事件处理程序中,您可以无限制地使用它。