简体   繁体   English

For 循环中的反应形式 patchValue()

[英]Reactive Forms patchValue() in For loop

Following sample code:以下示例代码:

model={name: "D100"
value: "Dying"};

   for (let key in model) {
            if(this.divisionForm.controls.hasOwnProperty(key)) {
                let value = divisionModel[key];
                this.form.patchValue({
                    key: value
                });
            }
        }
  • Is there any way to achieve the looping object and insert key value pair into the Angular reactive form's patchValue()有什么方法可以实现循环对象并将键值对插入到Angular 反应形式的patchValue() 中
  • I cannot have values to get set in form i can achieve this by manually add setValue() and use destructuring to get the objects and set which leads very long code in bigger forms.我无法在表单中设置值,我可以通过手动添加 setValue() 并使用解构来获取对象和设置,从而在更大的表单中引导非常长的代码来实现这一点。

I have structed in this for long time kindly help me.我已经在这方面建立了很长时间,请帮助我。 Thanks in advance.提前致谢。

Simply spread your form value and override the properties you want to change.只需传播您的表单值并覆盖您想要更改的属性。

As a sidenote, patchValue is supposed to keep the values of the keys you don't provide, eg giving a single key will keep all other keys to their actual value.作为旁注, patchValue应该保留您未提供的键的值,例如,提供单个键将使所有其他键保持其实际值。

Here is a stackblitz : https://stackblitz.com/edit/angular-xlxcic?file=src%2Fapp%2Fapp.component.ts这是一个堆栈闪电战: https ://stackblitz.com/edit/angular-xlxcic ? file = src%2Fapp%2Fapp.component.ts

  constructor() {
    const form = new FormGroup({
      name: new FormControl(''),
      surname: new FormControl('')
    });

    form.patchValue({
      ...form.value,
      name: 'name of the user'
    });

    form.patchValue({
      surname: 'surname of the user, with name kept'
    });

    console.log(form.value);
  }

try this, No need to use loop试试这个,不需要使用循环

this.form.patchValue(model)

For information visit, https://dev.to/crazedvic/using-patchvalue-on-formarrays-in-angular-7-2c8c有关信息,请访问https://dev.to/crazedvic/using-patchvalue-on-formarrays-in-angular-7-2c8c

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

相关问题 反应形式 patchValue() 与 ngValue 不起作用 - Reactive forms patchValue() with ngValue not working Angular 反应式 Forms,patchValue 不起作用,嵌套 forms - Angular Reactive Forms, patchValue not working, nested forms 在具有父子路由的Reactive Forms中使用patchValue - Use Of patchValue in Reactive Forms with parent child routing angular 补丁值 json 具有反应性的数据 forms - angular patchValue json data with reactive forms 如何在测试角度2反应形式中使用setValue和patchValue - How to use setValue and patchValue in testing angular 2 reactive forms Typescript:反应性 forms patchValue 不适用于私有属性和 getter/setter - Typescript: Reactive forms patchValue not working with private properties and getters/setters Angular 反应式 forms patchValue 对 ngbDatepicker 输入不起作用 - Angular reactive forms patchValue does not work on ngbDatepicker input Angular 反应形式 patchValue 或 setValue 不起作用 - Angular 10 - Angular reactive forms patchValue or setValue not working - Angular 10 Angular-Reactive Forms:如何在 patchValue 之后验证字段 - Angular- Reactive Forms: How to validate fields after patchValue Angular 反应性 Forms AddControl 在使用 PatchValue 时导致 ExpressionChangedAfterItHasBeenCheckedError - Angular Reactive Forms AddControl Causes ExpressionChangedAfterItHasBeenCheckedError When Using PatchValue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM