Angular 带字符串的修补程序值

Angular 带字符串的修补程序值,angular,Angular,如何用字符串修补字段值?由于某些原因,这不起作用: test = "myForm.controls.graphicUrl"; this[test].patchValue("hello"); 试着这样做: this.myForm.controls[graphicUrl].patchValue("hello"); 试着这样做: this.myForm.controls[graphicUrl].patchValue("hello"); 我想这就是你想要的: 如果有多个形式组,请考虑生成根窗体组

如何用字符串修补字段值?由于某些原因,这不起作用:

test = "myForm.controls.graphicUrl";
this[test].patchValue("hello");
试着这样做:

this.myForm.controls[graphicUrl].patchValue("hello");
试着这样做:

this.myForm.controls[graphicUrl].patchValue("hello");

我想这就是你想要的:

如果有多个形式组,请考虑生成根窗体组

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)

我想这就是你想要的:

如果有多个形式组,请考虑生成根窗体组

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)

如果你要投票否决,至少告诉我你的理由是什么。这是一个可以接受的问题。JavaScript不是这样工作的:在这个问题上没有反对票。我投票关闭,因为这里没有足够的信息进行调试。我们怎么知道什么“不起作用”呢?我个人用Lodash来做这样的事情:如果你要否决投票,至少告诉我你的理由是什么。这是一个可以接受的问题。JavaScript不是这样工作的:在这个问题上没有反对票。我投票关闭,因为这里没有足够的信息进行调试。我们怎么知道什么“不起作用”?我个人用Lodash来做这样的事情:谢谢,但这并不能回答问题。我必须通过一个字符串对其进行修补。好的,字符串中只能有lu
graphicUrl
。。。修改了答案。这能解决问题吗?谢谢,但这不能回答问题。我必须通过一个字符串对其进行修补。好的,字符串中只能有lu
graphicUrl
。。。修改了答案。这能解决问题吗?