0

i have a api responce like

res.attribute

0: {relate_id: 15, pid: 9, attr_slug: "size", items: "s,m,l", id: 1, …}
1: {relate_id: 16, pid: 9, attr_slug: "color", items: "yellow", id: 2, …}
length: 2

i need two set of radio buttons for size and color..i achived that through

 <section class="example-section"  *ngFor="let attr of Attr" >
      
        <mat-radio-button *ngFor="let checkbox of attr.items.split(',')" class="example-margin">{{checkbox}}</mat-radio-button>
    
    </section>

but, every radio button is like same name(only one from all 4 (s,m,l,yellow) can be selected)

what i am expecting is two different set of radio buttons like size and color

3 Answers 3

1

You can bind the input property name with the radio type you want.

[name]="attr.attr_slug"

This way it has a radio group that will be group by your slug name.

<section class="example-section"  *ngFor="let attr of Attr" >
      
        <mat-radio-button *ngFor="let checkbox of attr.items.split(',')" 
         [name]="attr.attr_slug"
         class="example-margin">{{checkbox}}</mat-radio-button>
    
    </section>
Sign up to request clarification or add additional context in comments.

1 Comment

i need those two selected values ...how is that possible?
1

You should be using a mat-radio-group to group up the radio buttons inside. Try it like below :

<section class="example-section"  *ngFor="let attr of Attr" >
    <mat-radio-group [name]="attr.attr_slug">
        <mat-radio-button *ngFor="let checkbox of attr.items.split(',')" class="example-margin">{{checkbox}}</mat-radio-button>
    </mat-radio-group>
</section>

Edit: For getting the desired "s,yellow" or "m,yellow" output you can do a workaround below.

Since your slugs are size and color. Define a model has those properties in your component:

model = {
size = "",
color = "",
}

Bind the radio group to the corresponding properties like below

<mat-radio-group [name]="attr.attr_slug" [value]="model[attr.attr_slug]">

So later in your component get your combined value like this :

const finalValue = `${this.model.size}, ${this.model.color}`

5 Comments

how to take selected values from this?
@Pugazh radio group has a property named value it gives you the selected value.
thanks , but i need a value like "s,yellow" or "m,yellow" (refer api response)
i tried using (click)="onClick(checkbox)" , where i am getting recently clicked button..but i need a pair of size and color
sorry @Eldar ..i am getting nothing...i have raised a detailed question ..please checkout stackoverflow.com/questions/67294497/…
0

As @Eldar said you need to include mat-radio-button inside mat-radio-group then you have two different sets of radio button groups. You can refer to the Angular Material docs here.

1 Comment

how to take selected values from this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.