重新加载 Angular 6 种基于最初选中的复选框值单击后退按钮的反应形式

Reload Angular 6 reactive form on back button click based on checkbox values checked initially

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

模板:

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

}  

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

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

Here is a slightly modified version of your example。保存 -> 更改复选框 -> 重新加载

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

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