如何用字符串修补字段值?由于某些原因,此操作无效:
test = "myForm.controls.graphicUrl";
this[test].patchValue("hello");
答案 0 :(得分:0)
尝试这样:
this.myForm.controls[graphicUrl].patchValue("hello");
答案 1 :(得分:0)
我认为这是您要寻找的: https://angular.io/api/forms/AbstractControl#get
如果您有多个FormGroup,请考虑制作一个根FormGroup
this.rootForm = this.fb.build({
myForm:this.myForm,
otherForm:this.otherForm
})
//then from your example:
const dotSeparatedPath = "myForm.graphicUrl"
this.rootForm.get(dotSeparatedPath).patchValue("hello")
//you can also pass an object as a parameter to the whole rootForm
const formValues= {
myForm:{
graphicUrl:"hello"
}
}
/* only graphUrl will be changes since the other keys in the object are not defined. */
this.rootForm.patchValue(formValues)