Angular 6反应形式输入类型=“无线电”互斥选择

时间:2019-02-11 15:06:21

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

我有Angular Dynamic表单,将从中加载服务中的数据。为了演示的目的,值在组件中进行了硬编码。 我有复选框和单选按钮。复选框功能很好。但是,使用单选按钮存在两个问题: 1)它们不是互斥的-不作为无线电组的一部分。如果我在HTML中添加name属性,则开发工具会抛出错误“如果在单选按钮上同时定义了name和formControlName属性,则它们的值       必须匹配。例如:“。 因此,我猜它在反应形式中不起作用。 2)我需要能够从此表单中来回移动(如向导),并且该表单应保留值。但是只有复选框保留了值,而不是单选按钮。为了演示的目的,有一个保存按钮,该按钮将值保存到data.service类,“重新加载最后保存”模仿了功能,以防我们需要来回移动并从data.service检索数据。

请指导! 模板:

<form [formGroup]="processDetailsForm" (ngSubmit)="submit()" *ngIf="isFormReady">
    <div class="row">
        <div class="form-group">
            <div class="col-xs-4 margin-bottom" formArrayName='ContactStreamEntries'
                 *ngFor="let HCS of processDetailsForm.controls.ContactStreamEntries.controls; let i = index">
                <input [formControlName]="i" type="radio" name="ContactStreamEntries[i].HCS"
                        [checked]="(i === 0)"
                        [value]="ContactStreamEntries.HCS_ID"
                       (change)="onSelectionChange(entry)"
                       /> {{ContactStreamEntries[i].HCS}}
            </div>
        </div>
    </div>
    <div class="row">
        <div class="form-group">
            <div class="col-xs-4 margin-bottom" formArrayName='ContactStreamsGroups'
                 *ngFor="let name of processDetailsForm.controls.ContactStreamsGroups.controls; let i = index">
                <input [formControlName]="i" type="checkbox" /> {{ContactStreamsGroups[i].name}}
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="row">
            <button type="submit" id="btnNext"
                    [disabled]="processDetailsForm.invalid">
                save
            </button>
            <button type="submit" id="btnNext" (click)="ReloadProcessDetails()">
                Reload last save
            </button>
        </div>
    </div>
</form>

组件:

import { Component, OnInit } from '@angular/core';
import { ReactiveFormsModule, FormBuilder, FormGroup, FormArray, FormControl, ValidatorFn } from '@angular/forms';
import { DataService } from '../../services/data.service';
import { Router } from '@angular/router';

@Component({
    selector: 'app-reports',
    templateUrl: './reports.component.html',
    styleUrls: ['./reports.component.scss']
})
export class ReportsComponent implements OnInit {
    processDetailsForm: FormGroup;
    isFormReady: boolean = false;
    private selectedAssets;
    //private selectedGroups;
    public dataProcessDetail;
     ContactStreamsGroups = [
         { id: 1, name: 'Group 1' },
         { id: 2, name: 'Group 2' },
         { id: 3, name: 'Group 3' }
     ];
    ContactStreamEntries = [
        { HCS_ID: 1, HCS: 'ABC' },
        { HCS_ID: 2, HCS: 'DEF' }
    ];
    constructor(private formBuilder: FormBuilder, private _dataService: DataService,
         private _router: Router) { }

    ngOnInit() {
        this.LoadControls();
        if (this._dataService.getBtnOption() === 'Back') {
            this.ReloadProcessDetails();
        }
    }
    submit() {
        this.selectedAssets = this.processDetailsForm.value;
        this.dataProcessDetail = {
            'selectedAssets': this.selectedAssets
        }
        this._dataService.setBtnOption('btnNextORBack', 'Next');

        this._dataService.setOptionProcessDetails('dataProcessDetail', this.dataProcessDetail);
        this._router.navigate(['forms']);
    }
    ReloadProcessDetails() {
        this.dataProcessDetail = this._dataService.getOptionProcessDetails();
        this.processDetailsForm.patchValue(this.dataProcessDetail.selectedAssets);
    }
    LoadControls() {
        const controlGroup = this.ContactStreamsGroups.map(c => new FormControl(false));
        const CS = this.ContactStreamEntries.map(c => new FormControl(false));

        this.processDetailsForm = this.formBuilder.group({
            ContactStreamsGroups: new FormArray(controlGroup),
            ContactStreamEntries: new FormArray(CS)
        });
        this.isFormReady = true;

    }
}

服务类别:

import { Injectable } from '@angular/core';
@Injectable({
  providedIn: 'root',
})
export class DataService {
  public btnData: string;
  public processData: any = null;

  setOptionProcessDetails(option, value) {
    this.processData = value;
  }
  getOptionProcessDetails() {
    return this.processData;
  }
  setBtnOption(option, value) {
    this.btnData = value;
  }
  getBtnOption() {
    return this.btnData;
  }
}  

0 个答案:

没有答案