Categories
Angular Answers

How to set dropdown options default value with Angular?

Spread the love

Sometimes, we want to set dropdown options default value with Angular.

In this article, we’ll look at how to set dropdown options default value with Angular.

How to set dropdown options default value with Angular?

To set dropdown options default value with Angular, we can add the selected attribute to the default option in the select.

For instance, we write

<select name="types" id="types" [(ngModel)]="model.type" #type="ngModel">
  <option class="" disabled selected value="undefined">Select an Option</option>
  <option *ngFor="let item of courseTypes; let x = index" [ngValue]="type.id">
    {{ item.name }}
  </option>
</select>

to make the first option the default one by adding the selected attribute to the option.

Conclusion

To set dropdown options default value with Angular, we can add the selected attribute to the default option in the select.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *