根据最初选中的复选框值,在后退按钮单击上重新加载Angular 6反应形式

时间:2019-02-06 22:31:16

标签: angular checkbox reload reactive-forms

以我的反应形式,我正在复选框中加载资产。单击“提交”按钮时,这些值将传递到_dataService服务类,并在路由到的下一个表单“表单”中检索。用户可以从“表单”中按下“后退”按钮,当重新加载此表单时,它应该能够填充最初检查的值。我可以在this.dataProcessDetail.selectedAssets中正确地从_dataService检索值,但是,我不知道如何检查复选框。我猜想如何将它们设置为true或false。这是完整的示例代码:

模板:

<form [formGroup]="processDetailsForm" (ngSubmit)="submit()" *ngIf="isFormReady">

    <div class="row">

        <div class="form-group">
            <label class="col-sm-12 control-label">
                Select 
            </label>
            <div class="col-xs-4" formArrayName='assets'
                 *ngFor="let option of processDetailsForm.controls.assets.controls; let i = index">
                <input [formControlName]="i" type="checkbox" /> {{assets[i].option}}
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <button type="submit" id="btnNext" class="btn btn-primary" [disabled]="processDetailsForm.invalid">
                Next >
            </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;
    public dataProcessDetail;
    assets = [
        { id: 1, option: 'chk 1' },
        { id: 2, option: 'chk 2' },
        { id: 3, option: 'chk 3' },
        { id: 4, option: 'chk 4' },
        { id: 5, option: 'chk 5' },
        { id: 6, option: 'chk 6' }
    ]; 
    constructor(private formBuilder: FormBuilder, private _dataService: DataService,
         private _router: Router) { }

    ngOnInit() {
        this.LoadControls();
        if (this._dataService.getBtnOption() === 'Back') {
            this.ReloadProcessDetails();
        }
        // this.LoadContactStreamsAssets();

    }
    submit() {
         this.selectedAssets = this.processDetailsForm.value.assets
            .map((v, i) => v ? this.assets[i] : null)
            .filter(v => v !== null);

        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();
        for (var i = 0; i < this.dataProcessDetail.selectedAssets.length; i++) {
            this.processDetailsForm.controls['assets'].setValue(
                this.processDetailsForm.controls['assets'].value
                    .map(value => this.dataProcessDetail.selectedAssets[i].option)
            );
            }
    }
    LoadControls() {
        const controls = this.assets.map(c => new FormControl(true));
        this.processDetailsForm = this.formBuilder.group({
            assets: new FormArray(controls)
        });
        this.isFormReady = true;

    }
}

..和data.service.ts

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;
    }

}  

2 个答案:

答案 0 :(得分:1)

只需将表单的值存储在服务中,然后使用patchValue

submit() {
    this.selectedAssets = this.processDetailsForm.value;
    ...
}
ReloadProcessDetails() {
    this.dataProcessDetail = this._dataService.getOptionProcessDetails();
    this.processDetailsForm.patchValue(this.dataProcessDetail.selectedAssets);
}

https://issues.apache.org/jira/browse/BEAM-6266。保存->更改复选框->重新加载

答案 1 :(得分:0)

您可以使用以下方法修补复选框值。

   ReloadProcessDetails() {
    this.dataProcessDetail = this._dataService.getOptionProcessDetails();
    this.processDetailsForm = this.formBuilder.group({
        assets:  this.formBuilder.array(this.assets.map(x => 
      this.dataProcessDetail.selectedAssets.indexOf(x.option) > -1))
    });
        }