简体   繁体   English

如何确保角度反应形式 setValue/patchvalue 是完整的

[英]How to make sure angular reactive form setValue/patchvalue is complete

if I set a form value like bellow如果我设置一个像波纹管这样的表单值

this.loadBasicForm.setValue({
       loadDirection,
       loadSubDirection,
       multiPointType,
       truckLoadType
});

I have to use setTimeout to make sure that the value is updated.我必须使用 setTimeout 来确保更新该值。

setTimeout(() => {
 //do some other action with the changed values.
}

Is there a better way to do this, other than using setTimeout.除了使用 setTimeout 之外,有没有更好的方法可以做到这一点。

Angular FormArray tracks the value and validity state of an array of FormControl, FormGroup or FormArray instances. Angular FormArray 跟踪 FormControl、FormGroup 或 FormArray 实例数组的值和有效性状态。 setValue() and patchValue() are the methods of FormArray class. setValue() 和 patchValue() 是 FormArray 类的方法。

setValue(): Sets the value of the FormArray. setValue():设置 FormArray 的值。 We need to pass an array that must match the structure of the control.我们需要传递一个必须匹配控件结构的数组。 If our array does not match the structure of the control, setValue() will throw error.如果我们的数组与控件的结构不匹配,setValue() 将抛出错误。

So, if you don't encounter any problem setValue() than the execution was done.因此,如果您没有遇到任何问题 setValue() 则执行已完成。

patchValue(): Patches the value of FormArray. patchValue():修补 FormArray 的值。 We need to pass an array that should match the structure of the control.我们需要传递一个应该匹配控件结构的数组。 If the array that we pass, does not match the structure of the control completely, then patchValue() will patch only those fields which are matching with the structure of the control and rest will be as it is.如果我们传递的数组与控件的结构不完全匹配,则 patchValue() 将只修补那些与控件结构匹配的字段,其余的将保持原样。

So,if your fields are not matching, it's the issue which you have made.因此,如果您的字段不匹配,则是您提出的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Angular 4中的反应形式控件分配给禁用,不确定如何或是否使用patchValue - Assigning a reactive form control in Angular 4 to disabled, not sure how or if to use patchValue 如何在测试角度2反应形式中使用setValue和patchValue - How to use setValue and patchValue in testing angular 2 reactive forms 如何以反应形式在ngselect中设置Value / PatchValue多个值 - How to setValue/PatchValue multiple value in ngselect in reactive form 不使用 setValue 或 patchValue 更新反应式表单字段 - Reactive form fields are not updated with setValue or patchValue Angular 11 Form PatchValue/SetValue 有时不工作 - Angular 11 Form PatchValue/SetValue is not working sometimes Angular 反应形式 patchValue 或 setValue 不起作用 - Angular 10 - Angular reactive forms patchValue or setValue not working - Angular 10 如何以Angular 2 Reactive形式对fb.array元素使用patchValue - How to use patchValue in Angular 2 Reactive form for fb.array elements Angular 9 反应形式; 带有嵌套 FormArray 的 FormGroup 的 PatchValue - Angular 9 Reactive Form; PatchValue of FormGroup with nested FormArray Angular 5 反应形式 - 带有 FormArray 的 patchValue 不起作用 - Angular 5 reactive form - patchValue with FormArray not working 角度反应形式错误的arrayGroup的patchValue - angular reactive form wrong patchValue of arrayGroup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM