Solution 1 :

To set a default value to mat-select, you have to bind compareWith property, which will compare the values and will set the default value.

Example:

In your HTML:

<mat-form-field class="full-width">
   <mat-select (selectionChange)="dropdownSelectedItem($event)" [(ngModel)]="dropdownSelectedName" [compareWith]="compareFn">
      <mat-option *ngFor="let item of itemList" [value]="item">
      {{item.name}}
      </mat-option>
   </mat-select>
</mat-form-field>

In your TS:

compareFn(obj1: any, obj2: any) {
    return obj1 && obj2 ? obj1.id === obj2.id : obj1 === obj2;
}

Solution 2 :

  • the value of your mat-option is a object reference to a item Object.
  • therefore the value that you set on mat-select must be the exact same item object reference.

this means that in your compoenent your code should

this.dropdownSelectedName = [Insert Object Reference Here]

the alternative approach is to change your mat-option to set

<mat-option *ngFor="let item of itemList" [value]="item.name">
  {{item.name}}
<mat-option>

with this change your “myDefaultValue” would match the value of one of the options, instead of being an object reference

Update

Here is a link that shows a working version of setting the value of the select when using object references.

Problem :

I am using mat-select within Angular Material as below:

<mat-form-field class="full-width">
   <mat-select (selectionChange)="dropdownSelectedItem($event)">
      <mat-option *ngFor="let item of itemList" [value]="item">
      {{item.name}}
      </mat-option>
   </mat-select>
</mat-form-field>

The above dropdown is coming up fine but the dropdown has no default value.

In order to add a default value, I have added value="{{dropdownSelectedName}}" as below:

<mat-form-field class="full-width">
   <mat-select (selectionChange)="dropdownSelectedItem($event)" value="{{dropdownSelectedName}}">
      <mat-option *ngFor="let item of itemList" [value]="item">
      {{item.name}}
      </mat-option>
   </mat-select>
</mat-form-field>

The above change is still not populating the default dropdown value.

I have also tried doing [(value)]="dropdownSelectedName"

When I do console.log(this.dropdownSelectedName) in the component, I can see that it has a string value
mydefaultvalue

ngOnInit() {
    this.subs.sink = this.myService.listAll()
        .subscribe(r => {
            this.AllList = r;
        });
    this.dropdownSelectedName = "mydefaultvalue";
}

Comments

Comment posted by meallhour

it is working fine exactly as expected. thanks. can you please help me understand what

Comment posted by Tushar

Basically, when we try to set some predefined value to mat-select, it will not work directly because of some reference issue, so it unable to set the value.

Comment posted by meallhour

but this doesn’t answer my question. I want to populate value of dropdown to the string

Comment posted by Edward

because you are setting the

Comment posted by meallhour

when i do

Comment posted by Edward

I have update the answer with a link to a working version in stackblitz

By