Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Angular dynamic flag icon in span based on mat-select change

I have a mat-select and I would like to change a flag icon in a span in the mat-label, based on the selected option.

The initial flag is shown, after a click no change occurs in the span.

The flags in the option list are working correctly, so the question only concerns the label.

I am trying something like:

HTML:

    <mat-form-field>
    <mat-label>
      <span [class]="getLanguageFlag(selectedLanguage)"></span>
      {{ selectedLanguage }}
    </mat-label>

    <mat-select
      [(ngModel)]="selectedLanguage"
      name="selectedLanguage"
      id="selectedLanguage"
    >

      <mat-option
        *ngFor="let language of languages"
        [value]="language"
        (click)="changeLanguage(language)"
      >

        <span [class]="getLanguageFlag(language)"></span>
        {{ language }}
      </mat-option>
    </mat-select>
  </mat-form-field>

TS:

    selectedLanguage: string = environment.defaultLanguage;

    constructor(
    private store: Store<State>,
    private translateService: TranslateService) {}

    changeLanguage(language: string): void {
    this.selectedLanguage = language;
    }

    getLanguageFlag(language: string): string {
    let style = 'flag-icon flag-icon-';
    return language === 'en' ? `${style}gb` : `${style}${language}`;
    }

I've also tried a getter/setter, but the setter didn't seem to work when using the mat-select.

Thanks in advance for your help!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I tested it and you code works. You should just check if 2-letter country code is correct.

I don't know if you just didn't copy it correctly, but you are missing closing <mat-form-field> tag in you HTML, so check that also.

Working example

about 4 years ago · Juan Pablo Isaza Report

0

The final solution, based on Nenad's help: (the flag before the option, no label above the select input)

<mat-form-field>
<span [class]="getLanguageFlag(selectedLanguage)"></span>
&nbsp;
<mat-select
  [(ngModel)]="selectedLanguage"
  name="selectedLanguage"
  id="selectedLanguage"
>
  <mat-option
    *ngFor="let language of languages"
    [value]="language"
    (click)="changeLanguage(language)"
  >
    <span [class]="getLanguageFlag(language)"></span>
    {{ language.toUpperCase() }}
  </mat-option>
</mat-select>
</mat-form-field>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!