且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角反应形式错误:必须为名称为表单的控件提供一个值:

更新时间:2022-12-10 10:51:35

如果您在formGroup上使用setValue,但没有为该组中的每个控件传递值,则可能会出现此错误.例如:

This error can appear if you're using setValue on a formGroup but not passing in a value for every control within that group. For example:

let group = new FormGroup({
  foo: null, 
  bar: null
});
group.setValue({foo: 'only foo'}); //breaks
group.setValue({foo: 'foo', bar: 'bar'}); //works

如果您确实只想更新组中的控件的 some ,则可以使用patchValue代替:

If you truly want to only update some of the controls on the group, you can use patchValue instead:

group.patchValue({foo: 'only foo, no problem!'});

setValuepatchValue的文档此处