Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
angular mat radio group select index

I have a radio button in a mat radio group coming from a loop , the button and card should be colored based on the selected index, but as you can see on the screenshot the radio button is selected but not colored in white

enter image description here

The radio button based on the selected index or selected radio should be like this but currently my logic does not work. Thanks for any help.

enter image description here

Any idea guys ? Thanks.

#html code

<p fxLayoutAlign="center" class="select-date-time-text">Select Available Date</p>
                <mat-radio-group>
                  <mat-card style="cursor: pointer;" [ngClass]="selectedIndex === i ? 'selected-schedule-card' : ''" *ngFor="let item of schedules;let i = index;" fxLayoutAlign="start center" style="cursor: pointer;margin-bottom: 20px;">
                    <mat-radio-button  color="accent"   [ngClass]="selectedIndex === i ? 'selected-schedule-radio' : ''"   value="i"  (click)="onSelect(item , i)">
                        <mat-icon class="date-icon" style="margin-top:3px;margin-left:10px;">today</mat-icon> {{item.proposedDateStartString}}
                      </mat-radio-button>
                  </mat-card>
              </mat-radio-group>

#tscode

    onSelect(item:any , index:number) {
    this.selectedIndex = index;
    this.selectedItem = item;
  }

#css

.selected-schedule-radio {
    color: #ffffff;
  }
  
  .selected-schedule-card {
    background-color: #007DFF;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Make a function on the ts side and use this on your html: (click)=“onSelect(item)”

about 4 years ago · Juan Pablo Isaza Relatório

0

You don't need a change event for this. Create a scope variable in your TS to hold the value, like

this.selectedDate='';

Put anngmodel binding on the mat-radio-group element.

[ngModel]="selectedDate"

That will track the selected value of the whole radio group. Then make your ngClass compare the radio's value with the selectedValue

[ngClass]="selectedDate === item.proposedDateStartString ? 'selected-schedule-radio' : ''"

All together...

<mat-radio-group [ngModel]="selectedDate">
  <mat-card style="cursor: pointer;" [ngClass]="selectedIndex === i ? 'selected-schedule-card' : ''" *ngFor="let item of schedules;let i = index;" fxLayoutAlign="start center" style="cursor: pointer;margin-bottom: 20px;">
    <mat-radio-button color="accent" [ngClass]="selectedDate === item.proposedDateStartString ? 'selected-schedule-radio' : ''" >
      <mat-icon class="date-icon" style="margin-top:3px;margin-left:10px;">today</mat-icon> {{item.proposedDateStartString}}
    </mat-radio-button>
  </mat-card>
</mat-radio-group>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda