Bug
When the ngModel of a mat-select is populated on page load the mat-select should have that option selected.
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.
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
material beta 12
angular 4.4.3
+1
well https://github.com/angular/material2/issues/6970#issuecomment-328355857 this
Hey @p0d3r, #6970 seems to be something else entirely. It describes a situation where the options are generated dynamically. The OP was surprised that the selected value was 'lost' when a new set op options was assigned.
This issue concerns strange behaviour if the displayed value of an option is modified by a pipe that has some async logic, like ngx-translate's 'translate' pipe. If the pipe is removed, everything works as expected.
I'm having the exact same issue.
I am also having the same issue with Reactive forms Module.. Does anybody found the solution?
The issue here comes from the fact that the displayed value of the select comes directly from the textContent of the option, however there's no way for the select to know whether that value has changed. It might be tricky to do this in a performant way, because it means that we'd either have to maintain a MutationObserver on each of the options in order to know that the text changed, or we'd have to check the textContent after every change detection cycle.
so basically... its an non-standard select html element which has the material design but less features? I'll stick to the default aka. normal select element then. doesn't make any sense not being able to show the default/selected at init.
It is possible to have a preselected value, it's currently not possible to have the label change asynchronously after init.
Is it possible to trim the selected value? e.g., i'm choosing a country phone code and want to show the full country name in the menu but only the code after selected? have hunted through several threads with nothing definitive. thanks
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
{{ option.value }}
```
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.
Hi, this solution I found is to set a value when we're going to initialize the view and also initializing our ReactiveForm, then when we retrieve the list of options with an Async call we reset the value in the "complete" callback:
ngOnInit() {
this.editForm = this.fb.group({
motif:[this.sanction.motif,[
Validators.required
]],
commentaire: [this.sanction.commentaire,[
]],
type: [null,[
Validators.required
]]
});
}
2.RESET:
constructor(public dialogRef: MatDialogRef<EditSanctionComponent>,
@Inject(MAT_DIALOG_DATA) public sanction: Sanction,
private fb: FormBuilder,
private typeSanctionService: TypeSanctionService
) {
console.log("CONSTRUCTOR EDIT SANCTION");
this.typeSanctionService.getTypeSanctionList().subscribe(results => {
this.typeSanctionOptions = results;
console.log("SUBSCRIBE OPTIONS----");
},
() =>{},
()=>{
console.log("ON COMPLETE CALL");
const typteToSelect = this.typeSanctionOptions.find(itemType=> itemType.id === this.sanction.type.id);
this.editForm.get('type').reset(typteToSelect);
});
}
Hope this will Help
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
_This action has been performed automatically by a bot._
Most helpful comment
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.