-1

When assigning ngModel to a template variable within an ngFor the value is undefined. It's been brought up on the Angular repo here but it's been over a year with seemingly no resolution. Does anybody have a solution or workaround? Seems like it should be a common issue.

    <tr *ngFor="let desc of descriptions; let i = index">
      <td>
        <my-input required name="description" #description="ngModel" [maxlength]="400" class="edit-input" type="text" [(ngModel)]="desc.description"></my-input>
        <label *ngIf="isRequired(description)" class="required-label">{{'required' | translate}}</label>
        <ng-template #readOnly><p>{{ desc.description }}</p></ng-template>
      </td>
    </tr>
Amerikaner
  • 131
  • 1
  • 1
  • 7

1 Answers1

0

To put a ngModel in a ngFor you have to make sure that each div has a unique name.

<tr *ngFor="let desc of descriptions; let i = index">
  <td>
    <my-input required name="description-{{i}}" #description="ngModel" [maxlength]="400" class="edit-input" type="text" [(ngModel)]="desc.description"></my-input>
    <label *ngIf="isRequired(description)" class="required-label">{{'required' | translate}}</label>
    <ng-template #readOnly><p>{{ desc.description }}</p></ng-template>
  </td>
</tr>
Lud
  • 286
  • 2
  • 10