簡體   English   中英

Angular patchValue不起作用

[英]Angular patchValue does not work

我正在嘗試在主窗體對象內更改一個對象。 我首先制作了一個簡單的fieldFormStructure,只是為了維護裸露的骨架。

     fieldFormStructure = this.fb.group({
            name: '',
            Properties: this.fb.group(
                {
                    referenceTypeData: this.fb.group(
                        {
                            'SourceId': this.fb.array([]),
                            'SourceIdString': '',
                        }),
                    imageTypeData: this.fb.group({
                        'ItemSelectType': '',
                        'Source': this.fb.group({
                            DAMSource: '',
                            ContentStudioSource: '',
                        })
                    }),
                }
            )
        });

然后在ngOnInit上初始化表單。

this.fieldForm = this.fieldFormStructure;

除非我嘗試對其進行一些更改,否則此方法效果很好。

在功能更改上,我基本上是在嘗試重置fieldForm。

this.fieldForm.get('Properties').patchValue(_.cloneDeep(this.fieldFormStructure.get('Properties').value));

在fieldForm中沒有任何變化。 怎么了

如果您有像這樣的表單組的示例

this.sampleForm=new FormGroup(
      {
        email:new FormControl(null,Validators.required),
        password:new FormControl(null,Validators.required),
        gender:new FormControl('male'),
        education:new FormGroup(
          {
            sslc:new FormControl('Pass'),
            hsc:new FormControl('Pass')
          }
        ),
        input:new FormArray([])
      }
    );

這是您必須設置補丁值的方式

 this.sampleForm.patchValue({
    email:'lachu@gmail.com',
    input:['data',''] //for array index 0 i am setting patch value and second 
    one i am setting null value 
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM