【问题标题】:Radio Buttons in angular dynamic forms角度动态形式的单选按钮
【发布时间】:2018-09-17 10:58:19
【问题描述】:

我是使用角度动态表单的新手,我遇到了单选按钮问题,当我选择单选按钮选项时,所选单选按钮的值不会反映在 FormGroup 的实例中。为了说明情况,我将向您展示相关代码:

我有两个组件:

  • 动态形式
  • 动态形式问题

dynamic-form 通过 toFormGroup(answers):FormGroup 方法接收答案列表,该答案列表在 dynamic-form-question 组件中呈现为单选按钮, dynamic-form 还构建了一个表单,该表单被传输到 dynamic-form-question 组件。

每个组件的代码下方:

dynamic-form.html

<form (ngSubmit)="onSubmit()" [formGroup]="form">
  <div *ngFor="let answer of answers$ | async">
    <app-question [answer]="answer" [form]="form"></app-question>
  </div>
</form>

dynamic-form.ts

    @Component({
  selector: 'app-dynamic-form',
  templateUrl: './dynamic-form.component.html',
  styleUrls: ['./dynamic-form.component.css'],
  providers: [ AnswerControlService ]
})
export class DynamicFormComponent implements OnInit {
  @Input() answers$: Observable<AnswerBase<any>[]>;
  form: FormGroup;
  payLoad = '';

  constructor(private qcs: AnswerControlService, private service: DynamicFormService) {  }

  ngOnInit() {
    this.form= new FormGroup({});
    this.answers$.subscribe(a=>this.form = this.qcs.toFormGroup(a));
  }

  onSubmit() {
    this.payLoad=JSON.stringify(this.form.value);
  }

dynamic-form-question.html

<div [formGroup]="form">
<label [attr.for]="answer.id">{{answer.label}}</label>

<div [ngSwitch]="answer.controlType">

  <input *ngSwitchCase="'radio'" [formControlName]="answer.controlName"
          [id]="answer.id" [type]="answer.type" [value]="answer.label">
</div> 
<div class="errorMessage" *ngIf="!isValid">{{answer.label}} is required</div>

dynamic-form-question.ts

   @Component({
  selector: 'app-question',
  templateUrl: './dynamic-form-question.component.html',
  styleUrls: ['./dynamic-form-question.component.css']
})
export class DynamicFormQuestionComponent {
  @Input() answer: AnswerBase<any>;
  @Input() form: FormGroup;
  @Input() currentQuestion:Question;

  get isValid() { 
    return this.form.controls[this.answer.controlName].valid;
  }
}

当我选择一个单选按钮选项时,会出现两个问题:

  1. 选项的选择不是相互排斥的,即使 answer.controlName 属性对于所有选项具有相同的值。

  1. 当我调用onSubmit() 方法时,选择了一些或所有选项。没有为控件分配值属性。接下来我给大家展示一下我的控制台在onSubmit()方法被调用时的例子。

我希望有人能帮我解决这个问题,因为我已经尝试了几个小时,但我不知道为什么会这样。非常感谢!

【问题讨论】:

  • 单选按钮组(具有相同名称属性)不可能允许多选。也许您已将单选按钮组放在不同的
    标记中?
  • @Michael Niño 我拥有的代码与这个问题的发布中的代码相同,就好像模板的控件与表单没有连接一样。不知道会不会是dynamic-form-question组件在构建的时候,一开始是用空表单构建的
  • 我遇到了同样的问题。有一些单选按钮连接到父 formGroup 的子组件,但值永远不会得到反映,在我的情况下,formgroup 作为 @Input 传递给子组件,但它也不起作用。

标签: angular typescript radio-button rxjs angular-reactive-forms


【解决方案1】:

使用与dynamic form documentation 中的下拉示例相同的行为

question-radio.ts

import { QuestionBase } from './question-base';

export class RadioQuestion extends QuestionBase<string> {
  controlType = 'radio';
  options: {key: string, value: string}[] = [];

  constructor(options: {} = {}) {
    super(options);
    this.options = options['options'] || [];
  }
}

question.service.ts

new RadioQuestion({
        key: 'sex',
        label: 'Sex',
        type: 'radio',
        options: [
          {key: 'Male',  value: 'm'},
          {key: 'Female',  value: 'f'}
        ],
        required: true,
        order: 4
      })

dynamic-form-question.html

<div *ngSwitchCase="'radio'">
      <label *ngFor="let opt of question.options">
        {{opt.key}}
        <input
          type="radio"
          [name]="question.key"
          [formControlName]="question.key"
          [value]="opt.value">
      </label>
    </div>

【讨论】:

  • 谢谢。我也有同样的挑战。我的表单是基于 JSON 文件的,但它或多或少是相同的逻辑
猜你喜欢
  • 2014-09-20
  • 2023-04-07
  • 2017-02-20
  • 1970-01-01
  • 2019-04-01
  • 2019-12-16
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多