【问题标题】:Angular How to patch value in the form arrayAngular如何修补表单数组中的值
【发布时间】:2021-03-25 07:05:59
【问题描述】:

我正在使用 Angular8 和响应式表单。我的 ngOnInit 是:

ngOnInit(): void {
  this.makeForm = this.fb.group(
    year: ['', Validators.required],
    amount: ['', Validators.required],
    desc: ['', Validators.required],
    details: new FormArray([
      this.det(),
    ]
  );
}

det() {
  return new FormGroup({
    for: new FormControl(''),
    remarks: new FormControl('')
  });
}

我有来自后端的数据来修补数组:

 {
  "amount": 9000,
  "year": 1996,
  "id": 1,
  "desc": "P1",
  "details": [
    {
      "id": 1,
      "remarks": "ok",
      "for": "1"
    },
    {
      "id": 24,
      "remarks": "OK",
      "for": "1"
    },
    {
      "id": 25,
      "remarks": "OK",
      "for": "1"
    }
  ]
 }

我通常用这种方法修补值:

getppredit() {
  let data:any = this.shareService.ParentShare.value;
  let id = data.id;
  this.dataService.getdataEdit(id)
    .subscribe((result: any) => {
      let no = result.no;
      let year = result.year;
      this.makeForm.patchValue({
        no: no,
        year: year
      });
  });
}

以上代码用于普通补丁,但是如何使用Angular8的反应形式动态地对数组内部的值进行补丁呢?

【问题讨论】:

    标签: angular angular-material angular8 angular-reactive-forms angular-forms


    【解决方案1】:

    在这里试试这个

    getppredit(){
      let data:any = this.shareService.ParentShare.value
      let id = data.id 
      this.dataService.getdataEdit(id)
        .subscribe((result: any)  => {
          let no = result.no;
          let year= result.year
          this.makeForm.patchValue({
              no     : no,
              year: year
              })
    
          //initialize empty array
          this.getFormArray() = new FormArray([]);
    
          //create needed formGroups and inside each formGroup all formControls
          for (let detail of result.details) {
            
            let idControl: FormControl = new FormControl('');
            let requestForControl: FormControl = new FormControl('');
            let remarkControl: FormControl = new FormControl('');
    
            idControl.setValue(detail.id);
            ForControl.setValue(detail.requestFor);
            remarkControl.setValue(detail.remark);
    
            this.getFormArray().push(new FormGroup({
              id: idControl,
              For: requestForControl,
              remark: remarkControl}));
           }
    
        })//end of subscribe
     
     }
    
        getFormArray(): FormArray{
            return this.makeForm.get('details') as FormArray;
          }
    

    表单数组通常旨在包含动态数据。因此,您必须首先在表单数组中创建动态表单控件(在收到响应后),然后将值放入其中。

    【讨论】:

    • 如何检测此请求中的值变化?
    • 如何推送到存在数组@Panagiwths Mpougioukos
    • @Madhav 现有数组可以是pushed to 使用array.push()。看我上面的回答
    • @Madhav 您使用我在回答 getFormArray() 中的这种方法来获取对现有数组的引用,然后使用 push 推送新元素
    • @Madhav 如果您希望 getFormArray().push(dat()) 使用您的 dat() 则可以使用,但是您将推送的表单将具有空值而不是来自 json 的值回复。如果你想拥有这些价值观,你必须看看我在答案中做了什么
    【解决方案2】:

    下面是我使用FormArray 的方法,它跟踪 FormControl、FormGroup 或 FormArray 实例数组的值和有效性状态

    export class Component implements OnInit {
    
      // initialize the FormArray with an empty array!
      formArray: FormArray = new FormArray([]);
      makeForm: FormGroup;
    
      ngOnInit() {
        this.makeForm = this.fb.group({
          no: ['', Validators.required],
          year: ['', Validators.required],
        });
      }
    
      getParEdit() {
        ...
        this.dataService.getDataEdit(id).subscribe(result => {
          const { no, year, details } = result;
          this.makeForm.patchValue({ no, year });
    
          if (details.length) {
            details.forEach(detail => {
              this.formArray.push(
                this.fb.group({
                  id: new FormControl({ value: detail.id }),
                  requestFor: new FormControl({ value: detail.requestFor }),
                  remark: new FormControl({ value: detail.remark }),
                });
              );
            });
          }
        });
      }
    }
    

    您可以为每个detail 迭代动态创建FormGroup,以及每个迭代中的FormControls。

    【讨论】:

    • 如何在这个数组中推送值? det(){ return new FormGroup({ requestfor : new FormControl(''), notes : new FormControl(''), // file_id : new FormControl(''), })}
    • 如何在现有数组中推送值
    • @Madhav 不确定您要问什么。那不是一个数组,那是一个FormGroup。我上面提供的示例显示了如何将FormGroup 推送到FormArray
    • 是的,我现在明白了,谢谢伙计,我从中学到了
    猜你喜欢
    • 2021-02-20
    • 2020-10-05
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-09
    • 2021-11-25
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多