简体   繁体   English

patchValue在FormArray上不起作用

[英]patchValue not working on FormArray

I am storing FormArray value in ngrx Store whenever any of the control's value changes. 每当控件的任何值更改时,我都将FormArray值存储在ngrx Store中。

Form Array has 47 Form Groups and each Form Group has a single Form Control. 表单数组有47个表单组,每个表单组都有一个表单控件。

I have a parent FormGroup which has 10 Form Arrays for 10 different data sets. 我有一个父FormGroup,其中有10个用于10个不同数据集的表单数组。 and those 10 Form Arrays can have various number of FormGroup with a single Form Control. 并且这10个表单数组可以通过单个表单控件具有不同数量的FormGroup。

What I have noticed is that when I change a value of a control. 我注意到的是,当我更改控件的值时。 I can see it changing in the valueChange event handler and this is where I am pushing the updated value in ngrx. 我可以在valueChange事件处理程序中看到它的变化,这就是我在ngrx中推送更新后的值的地方。

if I go to a different data set; 如果我转到其他数据集; I can see in console that master form has the updated value for the control that I changed. 我可以在控制台中看到,主表单具有我更改的控件的更新值。

However, if I come back to the same data set; 但是,如果我返回相同的数据集; valueChanges gets fired automatically and it is trying to set the older value for the control that I changed. valueChanges会自动触发,它正在尝试为我更改的控件设置较旧的值。

I have a guard clause to only continue within valueChanges if control is dirty wh ich works in normal situations however, it is creating a situation for me as this time the control is dirty and the older value is written and pushed to ngrx as well. 我有一个保护子句,仅当控件在正常情况下正常工作时才在valueChanges中继续运行,但是,这为我创造了一种情况,因为这次控件不干净并且较旧的值也被写入并推入了ngrx。

i need to figure out why valueChanges is getting triggered and why is it getting triggered with the older value when i can see that parent form did have the right value before re-visiting the dataset where I made the change. 我需要弄清楚为什么valueChanges会被触发,为什么它会被旧的值触发,当我在重新访问进行更改的数据集之前可以看到父窗体确实具有正确的值时。

On re-rendering, if I am checking if Form Array values exist in ngrx store or not. 在重新渲染时,如果我正在检查ngrx存储中是否存在表单数组值。 If yes then I patchValue the FormArray I have with the FormArray values that I previously stored upon value changes. 如果是,那么我使用先前在值更改时存储的FormArray值对我拥有的FormArray进行patchValue。

I can see that form array values coming ngrx store has the update value but when I call 我可以看到即将到来的ngrx存储的表单数组值具有更新值,但是当我调用时

this.formArray.patchValue(formArrayValue); this.formArray.patchValue(formArrayValue);

I see the valueChanges is called but with old values and after patchValue - this.formArray still have the old values. 我看到调用了valueChanges,但是使用了旧值,并且在patchValue之后-this.formArray仍然具有旧值。

Am I doing something wrong here or missing something. 我在这里做错什么还是错过了什么。

Code Snippets 代码段

this is how I am creating control within form group and adding them to form Array. 这就是我在窗体组中创建控件并将其添加到窗体Array的方式。

const fg: any = {};     
const rules = this.validationRules.filter(vc => vc.AttributeName.toLowerCase() === prop.key.toLowerCase());

     fg[key] = this.createControl(rules);
         if (value) {
                fg[key].setValue(value,
                 {
                    onlySelf: true,
                    emitEvent: false,
                    emitModelToViewChange: false,
                    emitViewToModelChange: false
                   });
            }

           const formGroup = new FormGroup(fg);
           this.controlsArray.push(formGroup);

This is how i am trying to patch Value my Form Array in ngOnChanges 这就是我试图在ngOnChanges中修补Value表格数组的方式

this.store.pipe(select(fromForms.getFormsState))
.pipe(take(1))
.subscribe(f => {
    const formArray = f.forms.forms[tableName];
    if (formArray) {
        this.controlsArray.patchValue(formArray);
    }
}); 

I will try to add some code snippets if that helps. 如果有帮助,我将尝试添加一些代码片段。

Thanks 谢谢

<mat-form-field>
   <input type="text" matInput placeholder="{{ label }}" [formControlName]="key" [readonly]="!isEditable" [(ngMode)]="value">
</mat-form-field>

I had [(ngMode)]="value" on the input control and I am building a Reactive form. 我在输入控件上有[(ngMode)]="value" ,并且正在构建一个Reactive表单。 Although my FormGroup did change but when the form got rendered it kept looking at the model which wasn't updated and hence it forced FormGroup to have the older value as well. 尽管我的FormGroup确实发生了变化,但是在呈现表单时,它一直在查看未更新的模型,因此迫使FormGroup也具有较旧的值。

So please make sure you check for unnecessary [(ngMode)]="value" . 因此,请确保检查不必要的[(ngMode)]="value"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM