How to correctly display icon inside the select drop down control using material select control. After selecting mat option its selecting text of icon as well how to overcome this issue ?
<mat-form-field>
<mat-select formControlName="genderFormControl" placeholder="Gender">
<mat-option>None</mat-option>
<mat-option *ngFor="let gender of genders" [value]="gender.value">
<mat-icon matListIcon>pregnant_woman</mat-icon>
{{gender.name}}
</mat-option>
</mat-select>
</mat-form-field>
We can use NgModel to get and set values in Select option for Angular Material Select as well as native Select.
You can get it done via the "mat-select-trigger"
option.
<mat-select-trigger>
<mat-icon>pregnant_woman</mat-icon> {{gender.name}}
</mat-select-trigger>
More Documentation on mat-select-trigger.
Complete code
<mat-form-field>
<mat-select formControlName="genderFormControl" placeholder="Gender">
<mat-option>None</mat-option>
<mat-option *ngFor="let gender of genders" [value]="gender.value">
<mat-icon matListIcon>pregnant_woman</mat-icon>
{{gender.name}}
</mat-option>
<!-- MUST USE mat-select-trigger TO SHOW mat-icon -->
<mat-select-trigger *ngIf="gender.value === 'f'">
<mat-icon>pregnant_woman</mat-icon> {{gender.name}}
</mat-select-trigger>
</mat-select>
</mat-form-field>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With