I am working on an Angular application using the Angular Material library.
component.html
<mat-select>
<mat-option *ngFor="let option of options" [value]="option.value">
<span> {{ option.name }} </span>
<span class="colorful-badge-class"> {{ option.label }} </span>
</mat-option>
</mat-select>
On the dropdown, the option works nicely as expected with each option having the option name and a coloured badge beside it. However, when the option is selected, the option is no longer styled inside the form control field.
In the inspected HTML, the selected field is shown as:
<span class="mat-select-min-line ng-star-inserted">option-name option-label</span>
Not even respecting the original DOM structure of the option.
Is there a way to choose what shows up in the form control field after the option is selected (e.g. just showing option.name
and not needing the label), or possibly style the selected field keeping the structure passed to the original <mat-option>
?
Any help is appreciated since there seems to be no documentation on the <mat-option>
component.
Work with
<mat-select-trigger>
element to customize the selected option text.selectedOptionObj
variable to store the selected option object.selectionChange
event to triggeronSelectOption
method for storing selected option object by selected value.Sample StackBlitz Demo
Reference
Select with custom trigger text