How to get control type in template driven form in Angular 4

Viewed 1986

I have a form in angular 4 component. The fields are generated dynamically, i.e., using ngFor to generate the inputs. The inputs are of different types, e.g. checkbox, text...

In the ts code, how can i know the type of the input control?

HTML

<form *ngIf="active" #easyForm="ngForm" (ngSubmit)="onSubmit()">
  <div *ngFor="let i in items">
    <input type="text" name="text-{{item.id}}" [(ngModel)]="item.text">
    <input type="checkbox" name="checkbox-{{item.id}} (ngModel)]="item.checked">
  </div>
</form>

TS

@ViewChild('easyForm') set content(easyForm: NgForm) {
this.easyForm = easyForm;
if (this.easyForm) {
  const keys = Object.keys(this.easyForm.controls);
  foreach(let key in keys) {
     this.easyForm.controls[key] <---- am i able to get the type of the control here?
  }
}
0 Answers
Related