1

我在使用 Anguarl FormArray 中的 ngSelect 下拉列表时遇到问题,我正在使用 FormArray 构建一个包含州和城市下拉列表的 FormGroup。

状态 ddl 会加载 oninit(),然后当我改变状态时,城市 ddl 应该加载各自的数据,但它不应该影响其他城市 ddl。

在此处输入图像描述

在选择尊重的州之前,不应加载其他城市下拉菜单。

我最初可以加载状态,但无法在状态更改时绑定 city ddl。这是我的代码。请看一看。

import { Component, OnInit } from "@angular/core";
import {
  FormBuilder,
  FormGroup,
  FormArray,
  AbstractControl
} from "@angular/forms";
import { debounceTime } from "rxjs/operators";

@Component({
  selector: "state-block",
  templateUrl: "./state-block.component.html",
  styleUrls: ["./state-block.component.scss"]
})
export class StateBlockComponent implements OnInit {
  typeLoadName: string;
  lFormGroup: FormGroup;
  states = [1, 2, 3, 4];

  stateControl: AbstractControl;
  stateFormControls: FormGroup;

  get lFormArray(): FormArray {
    return this.lFormGroup.get("lFormArray") as FormArray;
  }

  statelist = ["state1", "state2", "state3", "state4"];
  citylist = [];
  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.initControls();
  }
 
  initControls() {
    this.lFormGroup = this.fb.group({
      lFormArray: this.fb.array([])
    });
    this.states.forEach(element => {
      this.stateFormControls = this.createControls();

      const myFormArray = <FormArray>this.lFormGroup.get("lFormArray");

      this.stateFormControls.get("groupIndex").patchValue(myFormArray.length);

      this.stateFormControls.valueChanges.subscribe(data =>
        this.onValuesChanged(data)
      );

      this.lFormArray.push(this.stateFormControls);

      this.lFormArray.valueChanges.subscribe(value =>
        this.onFormArrayValueChanged(value)
      );

      const citylist = [
        "zonePeril1",
        "zonePeril2",
        "zonePeril3",
        "zonePeril4"
      ];

      const selectedStateControl = this.stateFormControls.get("state");
      selectedStateControl.valueChanges.subscribe(data => {
        this.stateFormControls.get("city").value = zonePerilList;
        
      });
    });
  }
  onFormArrayValueChanged(value: any): void {
    console.log("on form array value changed", value);
  }
  onValuesChanged(data: any): void {
    console.log("on value changed", data);
    
  }
  createControls() {
    return this.fb.group({
      groupIndex: "",
      state: "",
      city: "",
      
    });
  }
  
}
<form [formGroup]="lFormGroup">
  <div [formGroup]="lFormGroup">
    <div formArrayName="lFormArray">
      <div
        [formGroupName]="i"
        *ngFor="let item of lFormArray.controls; let i = index"
      >
        <div class="form-group row mb-2">
          <label class="col-md-2 col-form-label" for="cityId">State</label>
          <div class="col-md-3">
            <ng-select
              [items]="statelist"
              bindLabel="name"
              placeholder="Select state"
              formControlName="state"                         
            >
            </ng-select>
          </div>
          <label class="col-md-2 col-form-label" for="cityId">City</label>
          <div class="col-md-3">
            <ng-select
              [items]="citylist"
              bindLabel="name"
              placeholder="Select city"
              formControlName="city"
            >
            </ng-select>
          </div>
        </div>
      </div>
    </div>
  </div>
</form>

这还没有回答

4

1 回答 1

1

请检查 stackblitz 示例这是您想要的吗?

https://stackblitz.com/edit/ng-select-egj2ht

于 2019-11-24T20:40:47.093 回答