【问题标题】:Cannot set radio button value dynamically after second click第二次单击后无法动态设置单选按钮值
【发布时间】: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


【解决方案1】:

我也尝试了你所做的 Auqib 相反,这是我第一个解决问题的方法,但它仍然不能很好地工作,它发生了与操作相同的问题。第一次工作,下次不会更新。

我找到了解决方法,试试这个:

  <div class="container">
        <input type="radio" name="radio" (click)="radio = 'one'" id="one" class="" value="one" [checked]="radio == 'one'">
        <input type="radio" name="radio" id="two" (click)="radio ='two'" value="two" [checked]="radio == 'two'">
        <input type="radio" name="radio" id="three" (click)="radio ='three'" class="" value="three" [checked]="radio == 'three'">
        <button (click)=' onRadioDynamicChange()'>Click Me</button>
    </div>

ts:

  onRadioDynamicChange() {
        this.radio = "one";
  }

但我不知道为什么第一种方法没有按预期工作。如果有人知道原因,我会很高兴知道的。

【讨论】:

  • @eugensunic,Nico 你能提供一个关于同一问题的小提琴吗?会调查的
  • 我稍后再做。
  • 好的,我会接受你的解决方案。我的方法不起作用真是太神奇了我猜你不允许将其他单选按钮显式设置为 null/false。你应该只照顾目标人
【解决方案2】:

如果你想使用任何类型的对象,就像你在 [https://stackblitz.com/edit/angular-srfwdq?file=src%2Fapp%2Fapp.component.html] 你应该使用示例 'radioGroup' 对象作为 radioGroup:any={} 这样您就可以向它添加属性,如果您不确定对象属性天气是否存在,则在 html 中使用 optional 'radioGroup?.first'、'radioGroup?.second' 和 'radioGroup?.third'

您应该像这样使用并确保一次只有一个对象“radioGroup”为真 在ts中。

  export class AppComponent {
    name = 'Angular';
    radioGroup:any={first:false,second:false,third:false};


    onRadioDynamicChange() {
      this.radioGroup.second =false;
      this.radioGroup.third=false;
      this.radioGroup.first =true;
    }

  }

在html中

<form >

<input type="radio" id="one" name="a"  value="one" 
[checked] = "radioGroup.first" >First
<input type="radio" id="two" name="a"  value="two" 
[checked] = "radioGroup.second" >Second
<input type="radio" id="three" name="a"  value="three" 
[checked] = "radioGroup.third" >Third


<button type="button" 
(click)="onRadioDynamicChange()">first</button>

  </form>

它肯定会起作用

【讨论】:

  • 它不起作用,我不应该使用名称属性,因为我有角度形式--> formControlName
  • 你是否已经在 app.module.ts import { FormsModule,ReactiveFormsModule } from '@angular/forms' 中导入了这些模块;
  • 如果我没有导入它们,我的应用程序会在编译时抛出错误...是的,它们就在那里...我会在下午晚些时候尝试创建一个堆栈闪电战 gmt+2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 2022-10-15
  • 2018-03-09
  • 1970-01-01
相关资源
最近更新 更多