您如何使用无线电按钮在真实和错误值之间切换?使用角和反应性形式(formarray)

发布于 2025-02-08 11:25:13 字数 448 浏览 4 评论 0原文

这是我在formarray中拥有的广播按钮。

   <input
               
                type="radio"
                formControlName="correctAnswer"
                value=""
                name="correctAnswer"
                
              />

当选择收音机时,我希望我得到的价值是“ true”。当未选择时,我希望返回的值为“ false”。该值应将其保存在无线电输入本身中,以便以我的formarray值显示。

有什么想法吗?我考虑过进行复选框,但是一次只能选择一个。我基本上需要一个复选框,就像收音机一样,从某种意义上说,它只有一个formarray才是真实的。

提前致谢。

Here is the radio button that I have in a FormArray.

   <input
               
                type="radio"
                formControlName="correctAnswer"
                value=""
                name="correctAnswer"
                
              />

When the radio is selected, I want the value I get returned to be 'true'. When it's unselected, I want the value returned to be 'false'. This value is to be kept within the radio input itself, so that it displays in the value of my FormArray.

Any thoughts? I considered doing checkboxes, but I can only have one selected at a time. I basically need a checkbox that acts like a radio in the sense that it only one FormArray can be true.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2025-02-15 11:25:13

我认为,最好使用复选框:

<mat-checkbox formControlName="active" id="activeFilter">Active</mat-checkbox>

但是,如果您想使用RadioButton,则如果您在FormGroup中初始化此值,则可以使用。

this.formGroup = new FormGroup({
  active: new FormControl(false),
});

并指定html中的值:

<mat-radio-group aria-label="Select an option" formControlName="active">
  <mat-radio-button value="true">True</mat-radio-button>
  <mat-radio-button value="false">False</mat-radio-button>
</mat-radio-group>

In my opinion, it´s better use a checkbox:

<mat-checkbox formControlName="active" id="activeFilter">Active</mat-checkbox>

but if you want use a radiobutton maybe it will work if you initialize this value in your formGroup.

this.formGroup = new FormGroup({
  active: new FormControl(false),
});

And specify the values ​​in html:

<mat-radio-group aria-label="Select an option" formControlName="active">
  <mat-radio-button value="true">True</mat-radio-button>
  <mat-radio-button value="false">False</mat-radio-button>
</mat-radio-group>
晨曦÷微暖 2025-02-15 11:25:13

在某种意义上说,开关状态是true false,无线电按钮并不意味着保持真/错误。该值应存在或不存在 - 是否可以切换。我还建议使用此类操作的复选框(如果需要的话,看起来像无线电按钮)。但是...我们可以做这样的事情:

/**
 * @title Radios with ngModel
 */
@Component({
  selector: 'radio-ng-model-example',
  templateUrl: 'radio-ng-model-example.html',
  styleUrls: ['radio-ng-model-example.css'],
})
export class RadioNgModelExample {

  seasons: any[] = [
    { name: 'Winter', value: false },
    { name: 'Spring', value: false },
    { name: 'Summer', value: false },
    { name: 'Autumn', value: false },
  ];
  
}
<label id="example-radio-group-label">Pick your favorite season</label>
<br />
<ng-container *ngFor="let season of seasons">
  <mat-radio-button
    name="{{season.name}}"
    class="example-radio-button"
    [checked]="season.value === true"
    [value]="season.value"
    (click)="season.value = !season.value">
    {{season.name}} current value is {{season.value}}
  </mat-radio-button>
  <br />
</ng-container>

工作示例: https://stackblitz.com/edit/angular-lcrva2-cxphui?file=src%2fapp%2fradio-ng-model-example.html

如果您需要在广播广播电台上的formconconconconconconconcontrols按钮,每个广播按钮应在其自己的无线电组中,以进行模板驱动的表单控制,或使用反应性方法并从季节[]中读取值。

Radio buttons aren't meant to hold true/false values in a sense that the toggled state is true false. The value should be either present or not - toggled or not. I would also suggest checkbox for such operations (which could look like radio buttons if needs be). But... we could do something like this:

/**
 * @title Radios with ngModel
 */
@Component({
  selector: 'radio-ng-model-example',
  templateUrl: 'radio-ng-model-example.html',
  styleUrls: ['radio-ng-model-example.css'],
})
export class RadioNgModelExample {

  seasons: any[] = [
    { name: 'Winter', value: false },
    { name: 'Spring', value: false },
    { name: 'Summer', value: false },
    { name: 'Autumn', value: false },
  ];
  
}
<label id="example-radio-group-label">Pick your favorite season</label>
<br />
<ng-container *ngFor="let season of seasons">
  <mat-radio-button
    name="{{season.name}}"
    class="example-radio-button"
    [checked]="season.value === true"
    [value]="season.value"
    (click)="season.value = !season.value">
    {{season.name}} current value is {{season.value}}
  </mat-radio-button>
  <br />
</ng-container>

Working example: https://stackblitz.com/edit/angular-lcrva2-cxphui?file=src%2Fapp%2Fradio-ng-model-example.html

If you need formControls on the radio buttons, each radio button should be in within its own radio group for a template driven form control, or use reactive approach and read values from seasons[].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文