多个收音机组以一种角反应形式

发布于 2025-02-03 06:51:02 字数 341 浏览 4 评论 0 原文

我看到了此问题的答案,其中包括模板表格。但是,没有一个以反应性形式的formgroup来解决这种情况。

当我从页面上的一个表单中选择一个广播按钮时,它将取消选择默认的无线电按钮 以完全独立的形式和formgroup。为什么这是这样,我该如何修复? (我故意将所有内容分开,甚至是方法调用)

这是Stackblitz: stackblitz

I see answers for this issue with Template forms. But none that address this scenario with FormGroups in Reactive Forms.

When I select a radio button from one form on the page, it deselects the default radio button
in a completely separate form and FormGroup. Why is this, and how do I fix it? (I have deliberately separated everything, even the method call)

Here's the stackblitz:
Stackblitz

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

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

发布评论

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

评论(2

南城追梦 2025-02-10 06:51:02

收音机按钮按名称分组,在您的示例中,您没有提供任何名称,因此所有广播按钮均按单个无线电组分组。每个广播按钮的添加名称以避免取消选择其他按钮。

<div class="rb-wrapper">
  <input
    [id]="id"
    type="radio"
    [name]="name"
    [value]="value"
    [formControl]="ctrl"
    [checked]="checked"
  />
  <p>
    {{ label }}
  </p>
</div>

A radio buttons are grouped by name, In your example you are not providing any name so all the radio buttons are grouped as a single radio group.Add name to each radio button to avoid deselecting others.

<div class="rb-wrapper">
  <input
    [id]="id"
    type="radio"
    [name]="name"
    [value]="value"
    [formControl]="ctrl"
    [checked]="checked"
  />
  <p>
    {{ label }}
  </p>
</div>

Forked Working Example

巷雨优美回忆 2025-02-10 06:51:02

在基本HTML中,无线电按钮始终与 name 属性分组,一旦所有无线电按钮都具有相同的 name 属性,它们会形成无线电组
这是我们必须做的事情,请在 @input()中以> @input()中的 app-radio component中的组件中的属性,然后将其传递给其模板,当您想将其用作无线电组时,请传递同一组中的所有无线电按钮相同 name 值。

这是您的应用程序的分叉示例。
stackblitz

,这是我们必须做的一些小变化,使这项工作。

// app-radio.ts
export class RadioComponent implements OnInit {
 @Input() label: string;
 @Input() ctrl: FormControl;
 @Input() value: string;
 @Input() id: string;
 @Input() name: string;
 @Input() checked: string;

 ngOnInit() {
    console.log('ctrl', this.ctrl);
 }
}


// app-radio.html
<input
 [id]="id"
 [name]="name"
 type="radio"
 [value]="value"
 [formControl]="ctrl"
 [checked]="checked"
/>

// app.html
<form [formGroup]="frm">
<app-radio id="rc1" name="frm" label="Inputs" value="Inputs" [ctrl]="getRadioOptions()" checked></app-radio>
<app-radio id="rc2" name="frm" label="HTML" value="HTML" [ctrl]="getRadioOptions()" ></app-radio>
<app-radio id="rc3" name="frm" label="Code" value="Code" [ctrl]="getRadioOptions()" ></app-radio>
</form>
<hr>

<form [formGroup]="frm2">
<app-radio id="rc4" name="frm2" label="Inputs2" value="Inputs2" [ctrl]="getRadioOptions2()" checked ></app-radio>
<app-radio id="rc5" name="frm2" label="HTML2" value="HTML2" [ctrl]="getRadioOptions2()" ></app-radio>
<app-radio id="rc6" name="frm2" label="Code2" value="Code2" [ctrl]="getRadioOptions2()" ></app-radio>
</form>

In the basic HTML, the radio buttons are always grouped with name attribute, once all the radio buttons have the same name attribute they form a Radio Group.
Here's the thing we have to do, take the name property as @Input() in your app-radio component and pass it to its template, when you want to use it as Radio Group, pass all the radio buttons in same group a same name value.

Here's the forked example of your app with fix.
Stackblitz

And these are the few small changes we had to do, make this work.

// app-radio.ts
export class RadioComponent implements OnInit {
 @Input() label: string;
 @Input() ctrl: FormControl;
 @Input() value: string;
 @Input() id: string;
 @Input() name: string;
 @Input() checked: string;

 ngOnInit() {
    console.log('ctrl', this.ctrl);
 }
}


// app-radio.html
<input
 [id]="id"
 [name]="name"
 type="radio"
 [value]="value"
 [formControl]="ctrl"
 [checked]="checked"
/>

// app.html
<form [formGroup]="frm">
<app-radio id="rc1" name="frm" label="Inputs" value="Inputs" [ctrl]="getRadioOptions()" checked></app-radio>
<app-radio id="rc2" name="frm" label="HTML" value="HTML" [ctrl]="getRadioOptions()" ></app-radio>
<app-radio id="rc3" name="frm" label="Code" value="Code" [ctrl]="getRadioOptions()" ></app-radio>
</form>
<hr>

<form [formGroup]="frm2">
<app-radio id="rc4" name="frm2" label="Inputs2" value="Inputs2" [ctrl]="getRadioOptions2()" checked ></app-radio>
<app-radio id="rc5" name="frm2" label="HTML2" value="HTML2" [ctrl]="getRadioOptions2()" ></app-radio>
<app-radio id="rc6" name="frm2" label="Code2" value="Code2" [ctrl]="getRadioOptions2()" ></app-radio>
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文