简体   繁体   中英

Change data in form control Angular

I have an Angular v6 application. Here's my FormGroup

stepperForm: FormGroup;

constructor() {
  this.stepperForm = new FormGroup({
    apps: new FormControl(''),
    params: new FormControl('', SpecifyDetailsStepComponent.isValidJsonValidatorFn()),
  });
}

I have a method where I want to change the data in formControl params :

const stringData = JSON.stringify(jsonData);
this.stepperForm.setValue({
  apps: new FormControl(''),
  params: new FormControl(stringData, SpecifyDetailsStepComponent.isValidJsonValidatorFn()),
});

I have no error in the console but the data isn't changing at all.

Edit:

I need to be able to use the validator. Thank you

You may simply do :

this.stepperForm.get('params').setValue(stringData);

And then to take immediate effect for validators.

this.stepperForm.get("params").updateValueAndValidity();

Do via Form: this.stepperForm.get('FormControlName').setValue('WhateverData'); Do via FormControl: this.formControl.setValue('whateverData');

You can set multiple values at once using below one

this.stepperForm.setValue({
    apps: 'newValue',
    params: 'newValue',
});

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