1

I want to get the number of the radio buttons checked of a form to save that number and pass it to a progress bar.

<mat-radio-group name="clientID" [(ngModel)]="model.clientID">
    <mat-radio-button *ngFor="let n of CONSTANTS.CLIENT" [value]="n.value">
        {{n.display}}
    </mat-radio-button>
</mat-radio-group>

My progress bar is this:

progress bar image

The code of my progress bar is:

<div>
    <p>{{progressnumber}}%</p>
</div>
<mat-progress-bar mode="determinate" value="{{progressnumber}}"></mat-progress-bar>

In my .ts y have the number hardcoded

 progressnumber:number = 70;
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • 2
    As these are radiobuttons you can check only 1 (or none), am I right ? Or your radio values are numbers, and you want the checked number value ? If so, your radio-group should have a "value" attribute that contains the value you checked. – Flo Dec 19 '19 at 15:24
  • Maybe this will help = https://stackoverflow.com/questions/34997128/angular-2-get-values-of-multiple-checked-checkboxes – Inch High Dec 19 '19 at 15:27
  • @Flo you can check only 1 or none thats right :D and the values are not numbers – Brandon Reyes Dec 19 '19 at 15:30

1 Answers1

0

You can check for change event of radio buttons and according to that can fire a event and read value like below:

.ts

import { Component } from '@angular/core';
import { MatRadioChange } from '@angular/material';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

progressnumber:number = 70;
  clientID = 70; // I don't know what is your model.clientID is,
                 // I just use it clientID and fixed a initial value
                 // of it. You can use yours
  clients : any[] = [
    {value:70, display:'client 1'},
    {value:40, display:'client 2'},
    {value:50, display:'client 3'}
  ]

  radioChange(event : MatRadioChange){    
    this.progressnumber = event.value
  }

}

.html

<mat-radio-group name="clientID" [(ngModel)]="clientID">
    <mat-radio-button *ngFor="let n of clients" [value]="n.value" (change)="radioChange($event)">
        {{n.display}}
    </mat-radio-button>
</mat-radio-group>


<div>
        <p>{{progressnumber}}%</p>
    </div>
    <mat-progress-bar mode="determinate" value="{{progressnumber}}"></mat-progress-bar>

Working example:

https://stackblitz.com/edit/angular-material-m1

halfer
  • 19,824
  • 17
  • 99
  • 186
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44