简体   繁体   中英

How can I count the number of mat-radio-button checked? Angular

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM