繁体   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