【发布时间】:2018-11-06 13:14:40
【问题描述】:
我有一个用于动态选中或取消选中单选按钮的按钮。问题是它工作一次,然后如果我决定手动单击同一组的单选按钮(当然是另一个),它会按预期更新。但是,当我尝试将其动态调整为之前的值(单击按钮)时,它不再应用该值。
这里是代码
html:
<input type="radio" id="one" class="" formControlName="group" value="one" [checked] = "radioGroup.first" >
<input type="radio" id="two" class="" formControlName="group" value="two" [checked] = "radioGroup.second" >
<input type="radio" id="three" class="" formControlName="group" value="three" [checked] = "radioGroup.third" >
.ts
onRadioDynamicChange() {
this.radioGroup = { first: true, second: null, third: null };
}
所以,它会在我第一次单击 onRadioDynamicChange 时发生变化,它会检查第一个单选按钮。然后我点击它的兄弟,然后再次点击 onRadioDynamicChange,它不再检查第一个 radioButton,没有任何动作发生。
编辑
这是行为:Stackblitz example
【问题讨论】:
-
你在哪里调用 onRadioDynamicChange ?
-
在我的 html 中,按钮 (click)="onRadioDynamicChange"
-
有什么原因不能像docs 那样在
NgModel中使用单选按钮? -
@barbsan 我怀疑这是因为他的表单是反应式表单,将反应式表单与模型驱动表单混合是不可取的吗?
-
@eugensunic 而不是 value="one" 尝试 [value]="one"
标签: javascript html angular radio-button angular-forms