您可以尝试以下解决方法来解决“Angular formGroup。单选按钮不起作用。”的问题:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormControl } from '@angular/forms';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.html',
styleUrls: ['./your-component.css']
})
export class YourComponent implements OnInit {
formGroup: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.formGroup = this.formBuilder.group({
radioButton: new FormControl('')
});
}
}
请注意,formGroup必须与组件类中的属性名称相对应,并且formControlName必须与表单控件的FormControl名称相对应。
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormControl } from '@angular/forms';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.html',
styleUrls: ['./your-component.css']
})
export class YourComponent implements OnInit {
formGroup: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.formGroup = this.formBuilder.group({
radioButton: new FormControl('')
});
this.formGroup.get('radioButton').valueChanges.subscribe(value => {
console.log(value); // 处理单选按钮的值变化
});
}
}
通过订阅formGroup
中单选按钮的值变化,您可以执行适当的操作来处理单选按钮的值。
希望这些解决方法能帮助到您解决问题!