简体   繁体   English

Angular 如何默认设置单选按钮,反应式表单

[英]Angular how to default set radio buttons, Reactive forms

Implementing a reactive form in Angular with radio buttons.使用单选按钮在 Angular 中实现反应式表单。 I want to set a default value, I have tried the following:我想设置一个默认值,我尝试了以下方法:

Template:模板:

<form [formGroup]="form" (ngSubmit)="onSubmit(form)" class="actionsForm">

    <input type="radio" id="entertainCrowdSkill1" [value]="skillsForBackend[0]"
    class="radiobuttons" formControlName="skill" [checked]="true">

    <input type="radio" id="entertainCrowdSkill2" [value]="skillsForBackend[1]" 
    class="radiobuttons" formControlName="skill">

</form>

Model:模型:

  ngOnInit() {
    this.form = this.fb.group({
      skill: ['', Validators.required],
    });

  }

  onSubmit(form) {
    console.log(form.controls.skill.value) // logs empty string
}

The button appears to be checked even the CSS:checked class is applied.即使应用了CSS:checked类,该按钮似乎也被选中。 However, when I try to log the value of skill it is an empty string (the default value entered in the fb).但是,当我尝试记录技能值时,它是一个空字符串(在 fb 中输入的默认值)。 I have seen an answer but this was for template driven forms.我看到了一个答案,但这是针对模板驱动的表单。

How do I set a default value for the radio buttons in the template in a reactive form?如何以反应形式为模板中的单选按钮设置默认值?

Assign the initial value with the FormBuilder group contructor instead of using [checked].使用FormBuilder组构造函数而不是使用 [checked] 分配初始值。 You assigned the value '' , which is an empty string.您分配了值'' ,它是一个空字符串。 To execute the onSubmit function add (ngSubmit)="onSubmit()" to the form tag.要执行 onSubmit 函数,请将(ngSubmit)="onSubmit()"到表单标记中。

<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <input type="radio" formControlName="skill" [value]="skillsForBackend[0]">
  <input type="radio" formControlName="skill" [value]="skillsForBackend[1]">
</form>

In your typescript try the following.在您的打字稿中尝试以下操作。

ngOnInit() {
  this.form = this.fb.group({
    skill: [this.skillsForBackend[0], Validators.required]
  });
}

onSubmit() {
  console.log(this.form.value) // should show { skill: /* your data */ }
}

The below method worked for me.下面的方法对我有用。

First add value attribute in your html首先在你的 html 中添加 value 属性

<form [formGroup]="form" (ngSubmit)="onSubmit(form)" class="actionsForm">

    <input type="radio" id="entertainCrowdSkill1" [value]="skillsForBackend[0]"
    value="crowdSkill1"
    class="radiobuttons" formControlName="skill" [checked]="true">

    <input type="radio" id="entertainCrowdSkill2" [value]="skillsForBackend[1]" 
    value="crowdSkill2"
    class="radiobuttons" formControlName="skill">

</form>

And then modify your model as below.然后如下修改您的模型。

ngOnInit() {
    this.form = this.fb.group({
      skill: ['crowdSkill1', Validators.required],
    });

  }

  onSubmit(form) {
    console.log(form.controls.skill.value) // logs empty string
}

Notice how the value of the value attribute added in html is also added as the initial value of the skill in form group.注意在html中添加的value属性的值是如何也作为skill在表单组中的初始值添加的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM