question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mat-select not pre selecting a value when mat-option has async content

See original GitHub issue

Bug, feature request, or proposal:

Bug

What is the expected behavior?

When the ngModel of a mat-select is populated on page load the mat-select should have that option selected.

What is the current behavior?

mat-select does not show the current selected option on page load if a mat-option has some async behaviour, in my case the translate pipe:

        <mat-option *ngFor="let option of options" [value]="option.value">
          {{ option.label | translate }}
        </mat-option>

The current selected option is shown the moment the mat-select gets focus.

What are the steps to reproduce?

Please have a look at the following StackBlitz example: https://stackblitz.com/edit/angular-material2-issue-async-mat-option-label?file=app%2Fpet-select-async%2Fpet-select-async.component.ts

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

material beta 12 angular 4.4.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:15
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jraadtcommented, Dec 17, 2017

A terrible hack to use with dynamically loaded options that may or may not contain the currently selected item, but you want to show the select item info in the select trigger, is to always have the selected item as an option that is hidden.

<mat-option [value]="select.value" *ngIf="select.value" style="display: none">{{select.value}}</mat-option>

Or you could use [compareWith] and have it always return true as long as there is at least 1 option in your select box. Not sure which one is nastier.

2reactions
vovikdrgcommented, Dec 2, 2017

For now my workaround is like this Get labels

this.translateService.get(['Dancers', 'DancerIndexArchive']).safeSubscribe(this, (value) => {
     this.options.push({ key: 'dancers', value: value.Dancers });
     this.options.push({ key: 'dancerIndexArchive', value: value.DancerIndexArchive });
   });

Bind in Template

 <mat-form-field>
      <mat-select formControlName="Type">
        <mat-option [value]="option.key" *ngFor="let option of options">
          {{ option.value }}
        </mat-option>
      </mat-select>
    </mat-form-field>
Read more comments on GitHub >

github_iconTop Results From Across the Web

mat-select selected option not showing when ... - Stack Overflow
How do I go about making sure that selectedFood is pre-selected when options for the select component have been loaded?
Read more >
Angular Material2 Issue Async Mat Option Label - StackBlitz
Issue template for @angular/material.
Read more >
Select | Angular Material
Function to compare the option values with the selected values. The first argument is a value from an option. The second is a...
Read more >
TIL: How to use async pipe and populate mat-select
I was trying to use async | pipe directly on the view to get all the country list and populate my mat... Tagged...
Read more >
mat select all option stackblitz - You.com | The AI Search ...
Probably not. Open side panel. angular/componentsmat-select not pre selecting a value when mat-option has async content#7923. Created about 5 years ago.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found