You need the ngControl
attribute on the inputs you want to check.
<form #frm="ngForm" (ngSubmit)="save(frm)">
<input [(ngModel)]="user.name" #name="ngForm" ngControl="name" type="text">
<a (click)="showFrm()">Show Frm</a>
</form>
and in the component you can access the "frm" variable with
import {Component, ViewChild} from 'angular2/core';
...
@ViewChild('frm') public userFrm: NgForm;
...
public showFrm(): void{
console.log(this.frm);
}
You can't access the frm
in the constructor, it's not there at this moment, but in the ngAfterViewInit you can access it.
since Angular 8 or so they have updated the parameters for ViewChild. Currently I need to use this syntax:
@ViewChild('frm', { static: true })userFrm: NgForm;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…