如何查看表单数组中特定的 formGroup 是否有效

huangapple go评论53阅读模式
英文:

How to see if a specific formGroup in a formArray is valid

问题

我有一个formArray,我想检查每个单独的formGroup,以查看特定的组是否有效或无效(这样我就可以在有效的组上调用特定的函数,而在无效的组上不调用)。

当我执行<div>Form Group Valid?: {{roundInputs.get('{{roundCount}}')?.valid}}</div>时,它不会返回false或true(其中roundCount是ngFor中的索引)。然而,如果我执行<div>Form Group Valid?: {{roundInputs.get('0')?.valid}}</div>,其中0是索引,它会返回true/false(或用任何索引替换0,效果都相同)。

我认为问题可能是索引是一个数字,而roundInputs.get()正在寻找一个字符串,插值使用了一个数字?如果是这样的话,我的下一个想法是也许我需要创建一个自定义管道,它接受一个数字并将其转换为字符串?

如果有人有任何想法或比自定义管道更好的建议,将不胜感激。我有点惊讶Angular没有一个可以将数字转换为字符串的管道。

英文:

I have a formArray and I want to check each individual formGroup to see if that specific group is valid or not (so I can then have specific function called on the specific groups that are valid and not on the groups that are invalid).

When I do <div>Form Group Valid?: {{roundInputs.get('{{roundCount}}')?.valid}}</div> it doesn't return false or true (where roundCount is the index in the ngFor encompassing everything). However if I do <div>Form Group Valid?: {{roundInputs.get('0')?.valid}}</div> where 0 is the index then it returns true/false (or replace 0 with whatever index and it works all the same).

My thought is that the index is a number and the roundInputs.get() is looking for a string and the interpolation is using a number? If that's the case my next thought is maybe I need to create a custom pipe that takes in a number and converts it to a string?

If anyone has any ideas or a better suggestion than a custom pipe it would be much appreciated. I am a little surprised that Angular doesn't already have a pipe that transforms a number to a string

答案1

得分: 0

因为在

<div>表单组有效?:{{roundInputs.get('{{roundCount}}')?.valid}}</div>

你将 '{{roundCount}}' 作为一个字面字符串发送,所以 get 方法找不到索引。

作为一种解决方法,你可以使用

<div>表单组有效?:{{roundInputs.get(`${roundCount}`)?.valid}}</div>

或者

<div>表单组有效?:{{roundInputs.get(roundCount + '')?.valid}}</div>
英文:

Because in

<div>Form Group Valid?: {{roundInputs.get('{{roundCount}}')?.valid}}</div>

You're sending '{{roundCount}}' as a literal string so the get doesn't find the index.

As a workaround you can use

<div>Form Group Valid?: {{roundInputs.get(`${roundCount}`)?.valid}}</div>

or

<div>Form Group Valid?: {{roundInputs.get(roundCount + '')?.valid}}</div>

答案2

得分: 0

当将 FormGroup 推入 FormArray 时,您需要将其设置为强制项。Angular 会自动检查并更新表单数组。但如果您想手动进行操作

1. 首先,通过 getter 获取 `roundInputs` 作为表单数组

```typescript
get getRoundInputs() {
    return this.roundInputs as FormArray;
}
  1. 然后遍历数组
this.roundInputs.controls.forEach((control) => {
    //将控件转换为 FormGroup
});

这将提供类型安全的表单组。


<details>
<summary>英文:</summary>

When pushing FormGroup into FormArray, you need to make it mandatory. Angular checks and updates form array automatically. But if you want to do manually

1. First, get `roundInputs` as form array via getter

get getRoundInputs() {
return this.roundInputs as FormArray;
}


2. Then iterate over the array

this.roundInputs.controls.forEach((control) => {
//cast control as Form Group
});


This will give type safe form group. 

</details>



huangapple
  • 本文由 发表于 2023年3月23日 10:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定