如何使用Angular5 / 6显示下拉列表选择的项目?

时间:2018-10-17 19:03:15

标签: javascript angular typescript angular5

我想根据ID在下拉列表中显示选定的项目。

<div class="formrow" *ngFor='let a of applicantName'>
  <label>Status Selection :</label>
  <select id="AStatus" formControlName="AStatus" class="form-control">
    <option value="0">All</option>
    <option *ngFor="let apt of applicantStatus" [value]="apt.ApplicantStatusID" [selected]="apt.ApplicantStatusID === a.Status">
      {{ apt.ApplicantStatus }}
    </option>
  </select>
</div>

此代码不起作用,并且未显示所选选项。

1 个答案:

答案 0 :(得分:1)

value应该设置为select not for option。

<div class="formrow" *ngFor='let a of applicantName'>
  <label>Status Selection :</label>
  <select id="AStatus" [value]="apt.ApplicantStatusID" formControlName="AStatus" class="form-control">
    <option value="0">All</option>
    <option *ngFor="let apt of applicantStatus" [value]="apt.ApplicantStatusID">
      {{ apt.ApplicantStatus }}
    </option>
  </select>
</div>