0

I want to display selected item in Dropdown list based on 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>

This code is not working and not showing selected option.

4
  • I'd bind the dropdown value to a ngModel and this model can be set with the value of id Commented Oct 17, 2018 at 19:06
  • well, you can use the ngModel and then it will simply set the selected on the value. Commented Oct 17, 2018 at 19:07
  • You can make use of data-binding on the selected option model. Commented Oct 17, 2018 at 19:07
  • Show us your ts, how is AStatus defined? Did you try [formControlName]=....? Commented Oct 18, 2018 at 0:33

1 Answer 1

1

value should be set for 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>
Sign up to request clarification or add additional context in comments.

3 Comments

The value must also be set on the options. Otherwise, the select element cannot match its value with the corresponding option.
That make sense. so you can add in both places and check
Updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.