设置在单选按钮组中选中的角度材质单选按钮

时间:2021-03-16 22:32:55

标签: javascript angular angular-material

我有一个 Angular Material Radio Button Group,里面有三个单选按钮。我需要检查其中一个按钮。我以为设置单选按钮组的值会检查相应的单选按钮,但事实并非如此。这就是我所拥有的。

HTML 模板

        <mat-radio-group  [formControl]="rbgMarkType" name="MarkType">
            <mat-radio-button value=1>Marked In Field</mat-radio-button>
            <mat-radio-button value=2>No Conflict</mat-radio-button>
            <mat-radio-button value=3>Cancel</mat-radio-button>
        </mat-radio-group>

打字稿代码,试图检查单选按钮:

export class MtMarkFormComponent implements OnInit {
  ...
  rbgMarkType:FormControl = new FormControl(); 
  ...


ngAfterViewInit() {
 this.SetMarkType();
}

SetMarkType(){
  this.rbgMarkType.patchValue({MarkType:3});//sets value but doesn't check radio button
}
...
}

谢谢

皮特

1 个答案:

答案 0 :(得分:1)

您已经将 rbgMarkType 作为 formControl

name="MarkType" 仅用于 <mat-radio-group>

的名称
SetMarkType(){
  this.rbgMarkType.patchValue('3');
}