Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display icon inside angular material select <mat-option> and selection of the same

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>

enter image description here


enter image description here

like image 714
Mark Macneil Bikeio Avatar asked Sep 16 '18 05:09

Mark Macneil Bikeio


People also ask

Can we use NgModel in mat select?

We can use NgModel to get and set values in Select option for Angular Material Select as well as native Select.


2 Answers

You can get it done via the "mat-select-trigger" option.

  <mat-select-trigger>
      <mat-icon>pregnant_woman</mat-icon>&nbsp;{{gender.name}}
   </mat-select-trigger>

More Documentation on mat-select-trigger.

like image 174
Sajeetharan Avatar answered Sep 22 '22 09:09

Sajeetharan


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>&nbsp;{{gender.name}}
        </mat-select-trigger>
    </mat-select>
</mat-form-field>
like image 39
Reza Taba Avatar answered Sep 18 '22 09:09

Reza Taba