首页 > 解决方案 > 如何计算检查的垫子单选按钮的数量?角

问题描述

我想检查表单的单选按钮的数量以保存该数字并将其传递给进度条。

<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>

我的进度条是这样的:

进度条图片

我的进度条代码是:

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

在我的 .ts y 中有硬编码的数字

 progressnumber:number = 70;

标签: javascripthtmlangular

解决方案


您可以检查单选按钮的更改事件,并据此触发事件并读取如下值:

.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>

工作示例:

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


推荐阅读