0
votes

I am trying to generate form dynamically . I am trying to set element reference to ngModel dynamically but it is not working .

 #{{item.name}}="ngModel"

.Here is the code

  <input type={{item.type}} class={{item.className}} placeholder="{{item.placeholder}}"
  [(ngModel)]="item.value" name='{{item.name}}' #{{item.name}}="ngModel"
  [ngClass]="{ 'is-invalid': f.submitted && item.value==''}" [required]="item.required">

Json

{
className: "form-control"
icon: "fa-font"
label: "TextBox"
layout: "row"
layoutList: (2) [{…}, {…}]
name: "textq6es58"
placeholder: "Enter your name"
required: true
toggle: true
type: "text"
}

Is there any way to set Dynamic element reference in angular template driven forms?

1
it's not neccesary. use simply #item I suppose your'e using in a *ngFor. Angular "create" a different template reference (really the reason is that Angular take account the scope, and the scope is different) - Eliseo

1 Answers

0
votes

You can provide it as

[id]='variableName'

so that the variableName will be the element reference ID.