【问题标题】:Template variable is undefined when using ngFor使用 ngFor 时模板变量未定义
【发布时间】:2018-07-24 20:17:02
【问题描述】:

将 ngModel 分配给 ngFor 中的模板变量时,该值未定义。它已在 Angular 存储库 here 中提出,但已经一年多了,似乎没有解决方案。有人有解决方案或解决方法吗?看来这应该是一个普遍的问题。

    <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>

【问题讨论】:

标签: angular ngmodel


【解决方案1】:

要将 ngModel 放入 ngFor 中,您必须确保每个 div 都有唯一的名称。

<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>

【讨论】:

  • 仍然返回未定义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
相关资源
最近更新 更多