Angular Material md-select default selected value

ErnieKev picture ErnieKev · Feb 9, 2017 · Viewed 50.2k times · Source

I am using Angular 2 with Angular Material. Looking at the documentation, I am trying to get the select to have a default value as oppose to an empty place holder.

I have tried the following two options and both of them does not set a default value

<md-select selected="1">
  <md-option value="1" >One</md-option>
  <md-option value="2">Two</md-option>
</md-select>

<md-select>
  <md-option value="1" selected="true">One</md-option>
  <md-option value="2">Two</md-option>
</md-select>

I looked at all the documentations and the examples, non of them helped

Answer

developer033 picture developer033 · Feb 10, 2017

Use [(ngModel)]:

<mat-select [(ngModel)]="selectedOption">
  <mat-option value="1">One</mat-option>
  <mat-option value="2">Two</mat-option>
</mat-select>

Component:

selectedOption = '1';

DEMO


Edit #1:

Since Material2.0.0#beta10 (specifically this PR) you can select a value using the value property of MatSelect:

<mat-select [value]="selectedOption">
  <mat-option value="1">One</mat-option>
  <mat-option value="2">Two</mat-option>
</mat-select>

Component:

selectedOption = '1';

Note that you can also use it with two-way data binding -> [(value)].

DEMO