您如何使用无线电按钮在真实和错误值之间切换?使用角和反应性形式(formarray)
这是我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,最好使用复选框:
但是,如果您想使用RadioButton,则如果您在FormGroup中初始化此值,则可以使用。
并指定html中的值:
In my opinion, it´s better use a checkbox:
but if you want use a radiobutton maybe it will work if you initialize this value in your formGroup.
And specify the values in html:
在某种意义上说,开关状态是true false,无线电按钮并不意味着保持真/错误。该值应存在或不存在 - 是否可以切换。我还建议使用此类操作的复选框(如果需要的话,看起来像无线电按钮)。但是...我们可以做这样的事情:
工作示例: 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:
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[]
.