简体   繁体   English

Angular-FormArray,如何重置formArray的特定字段?

[英]Angular - FormArray, how to reset a specific field of a formArray?

I can't figure out how to reset a single field of a FormArray, for example: 我不知道如何重置一个FormArray的单个字段,例如:

myForm = this.fb.group(
        {
            title: ["", [Validators.required, Validators.minLength(4)]],
            date: ["", Validators.required],
            pairs: this.fb.array(
                this.fPairs.map(f =>
                    this.fb.group({
                        score: [],
                        value: [],
                        valueRel: []
                    })
                )
            )
        },
        {
            validator: minMaxValidator
        }
    );

so my FormArray is an Array of 4 Objects as it's mapped from: 所以我的FormArray是4个对象的数组,因为它是从以下位置映射的:

fPairs: Array<fPairs> = [
        {score: 0, value: 0, valueRel: 0},
        {score: 0, value: 0, valueRel: 0},
        {score: 0, value: 0, valueRel: 0},
        {score: 0, value: 0, valueRel: 0}
    ];

what I achieved so far, to reset this part of my form, is: 到目前为止,重设表单的这一部分所取得的成就是:

pairsControl= this.myForm.controls["pairs"] as FormArray;

and then use: 然后使用:

this.pairsControl.reset(); this.pairsControl.reset();

but this resets EVERY field of the FormArray, and what I want instead, is being able to reset only a specific field, 但这会重置FormArray的每个字段,而我想要的是只能重置特定字段,

for example the " score" field of all 4 objects, while leaving intact the value and the valueRel fields 例如所有4个对象的“ score"字段,而值和valueRel字段保持不变

I tried this: 我尝试了这个:

this.fixedPointsControl.reset(["score"]);

but what it does, is reset everything like the previous expression, so nothing changes! 但是它的作用是重置所有与上一个表达式类似的内容,因此没有任何变化!

What s the correct way to reset a specific field of a formArray? 重置formArray的特定字段的正确方法是什么?

If you have 4 fields inside your FormArray, you can reset it through its index since inside a FormArray is an Array of FormGroup so you can access it by its index. 如果您的FormArray内部有4个字段,则可以通过其索引重置它,因为FormArray内部是一个FormGroup数组,因此您可以通过其索引访问它。

Had created a Stackblitz Demo for your reference 已经创建了Stackblitz演示供您参考

const pairs = this.myForm.get['pairs'] as FormArray;   // Use 'get' instead of 'controls'

// Iterates the Pairs' FormArray controls and use patchValue if you want to reset or assign a new value to a particular property
pairs.controls.forEach(pair => pair.patchValue({ score: '' }));

Just create a pairs getter to get the pairs FormArray . 只需创建一个pairs获取器即可获取pairs FormArray Now in the resetField method set a parameter named fieldName that would expect the name of the filed on the pairs FormArray to reset 现在,在resetField方法中设置一个名为fieldName的参数,该参数希望pairs FormArray pairs FormArray进行reset

Something like this: 像这样:

get pairs() {
  return (<FormArray>this.myForm.get('pairs'));
}

resetField(fieldName) {
  this.pairs.controls.forEach(group => group.get(fieldName).reset());
}

And then add these buttons to your template: 然后将这些按钮添加到您的模板中:

<button (click)="resetField('score')">Reset Score</button> | 
<button (click)="resetField('value')">Reset Value</button> | 
<button (click)="resetField('valueRel')">Reset Value Rel</button>

Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

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

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