Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 初始化并获取组件上的单选按钮值_Angular - Fatal编程技术网

Angular 初始化并获取组件上的单选按钮值

Angular 初始化并获取组件上的单选按钮值,angular,Angular,问题:在这种情况下,如何获取组件初始化时的无线电输入值 组成部分: colors = ['red', 'blue']; selectedColor: FormControl; ngOnInit() { this.selectedColor = new FormControl(); } ngAfterViewChecked() { console.log(this.selectedColor.value) // return null on component initialaz

问题:在这种情况下,如何获取组件初始化时的无线电输入值

组成部分:

colors = ['red', 'blue'];
selectedColor: FormControl;

ngOnInit() {
    this.selectedColor = new FormControl();
}
ngAfterViewChecked() {
    console.log(this.selectedColor.value) // return null on component initialazation
}
模板:

<ng-container *ngFor="let c of colors,  let i = index">
  <input
    [formControl]="selectedColor"
    type="radio"
    name="group"
    [id]="c"
    [value]="c"
    [checked]= "i === 0">
  <label attr.for="{{c}}">{{c}}</label>
</ng-container>

{{c}}
现在,如果我尝试在
ngAfterViewChecked()
中获取选中输入的值,该输入的
“I==0”
条件等于true,则在组件初始化时获取null,但在无线电输入更改时返回正确值。


<input
    type="checkbox"
    class="form-control"
    id="'someRandomId'"
    name="'someRandomId'"
    [ngModel]="value"
    (ngModelChange)="changeValue()">
  <label  [attr.for]="'someRandomId'"><span *ngIf="name">{{name}}</span></label>
{{name}}
我正在考虑这里的用例。不使用ngModel的一个原因?两者的工作没有区别,但在这两种情况下,它们都需要首先从组件获取检查的无线电输入的初始值,我尝试做的是相反的方向,我想从模板到组件获取检查的无线电输入的值