简体   繁体   中英

Angular patchValue does not work

I am trying to change an object inside my main form object. I made a simple fieldFormStructure first, just to maintain the bare skeleton.

     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: '',
                        })
                    }),
                }
            )
        });

And then on ngOnInit I initialize the form.

this.fieldForm = this.fieldFormStructure;

And this works perfectly fine, unless I try to make some changes in it.

On a function change, I am basically trying to reset fieldForm.

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

And nothing changes in fieldForm. What is wrong here?

Example if you have form group like

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 is how you have to set patch value

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM