1
votes
`<mat-radio-group [ngClass]="cssForGroup" name="test">
      <mat-radio-button *ngFor="let option of options | filter:searchText" 
                class="cssForRow"
                [value]="option" 
                [checked]="option.isSelected"
                (change)="selectOption($event)">         
                        {{ option.value }}
                        <div class="info" [hidden]="!option.info">{{option.info}}</div>
            </mat-radio-button>
 </mat-radio-group>`

This the code I have for Radio buttons in a Group using angular material where option is an object. I have an issue where the option.value can be same for multiple options but the option.info is different. But when I select a radio button of such a case. All the buttons with the same option.value gets selected on UI.

I have tried adding name attribute to mat-radio-group. I have changed value attribute from an object to a unique key. I tried trackBy in ngFor. These solutions did not fix it.

Can somebody help me fix this?

2

2 Answers

0
votes

"I have an issue where the option.value can be same for multiple options but the option.info is different".

Then you can not use a mat-radio-button. futhermore, you've never know what value are you clicked. I must suppose that you has an array of options

//"WRONG" I don't know if you push the first or the last option
options=[{value:1,info:'one'},{value:2,info:'two'},{value:1,info:'one bis'}]

you must use

//"OK", If I choose the first option the value is 101, value%100=1
//      If I choose the last option the value is 201, value%100=1
//         and know that Is the last option
     
optionsValors=[{value:101,info:'one'},{value:102,info:'two'},{value:201,info:'one bis'}]

//or

optionsValors=this.options.map((x,index)=>
     ({value:index*100+x.value,info:x.info}))

Remember use the module operator if you want the "real value"

value=value%100

If your options.value are strings you can make some similar adding a letter and use substr

NOTE: I imagine that you want to store in a variable the value of the options. Rememeber that this is Angular!! so, why not use [(ngModel)]?

<mat-radio-group [ngClass]="cssForGroup" name="test" [(ngModel)]="test">
      <mat-radio-button *ngFor="let option of options | filter:searchText" 
                class="cssForRow"
                [value]="option" >         
                        {{ option.value }}
                 <div class="info" [hidden]="!option.info">{{option.info}}</div>
       </mat-radio-button>
 </mat-radio-group>
0
votes

My options object also had a key called id. Id was not set to be unique for all objects in the array. (A miss from my side.) So when I set unique ids to the each object in the array. It worked. I am using event / api from mat-radio-button for working with selections. Hence, I did not have to use ngModel. (But is that a good practise!!?? But the architectural design for this angular project is way different. Different question.)

But I still have an unexplained behavior here. I am not using any means to tell mat-radio-button about id. Then how did that pose a problem for it. Maybe [value] and not [ngValue] caused a problem in the DOM or the internal working of mat-radio-button but I am still using [value] and somehow a unique key 'id' solved it.