Angular - 在使用FormArray循环时获取响应式表单的错误
创始人
2024-10-15 03:00:16
0

当使用FormArray来循环响应式表单时,可以使用以下解决方法来获取错误:

  1. 在HTML模板中,使用*ngFor循环formArray.controls来获取每个表单控件的错误信息。例如:
Field is required
Field should have at least 5 characters
  1. 在组件中,使用get方法获取表单控件的错误信息。例如:
import { Component } from '@angular/core';
import { FormArray, FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent {
  myFormArray: FormArray;

  constructor() {
    this.myFormArray = new FormArray([
      new FormGroup({
        myControl: new FormControl('', [Validators.required, Validators.minLength(5)])
      }),
      new FormGroup({
        myControl: new FormControl('', [Validators.required, Validators.minLength(5)])
      })
    ]);
  }

  getControlErrors(control: FormControl) {
    return control.errors;
  }
}

在HTML模板中,使用getControlErrors方法来获取控件的错误信息。例如:

Field is required
Field should have at least 5 characters

这些方法可以帮助你在使用FormArray循环时获取响应式表单的错误信息。

相关内容

热门资讯

重大通报!wepoker怎么挂... 重大通报!wepoker怎么挂飞机,wepoker有没有机器人,2025新版技巧(有挂透明)1、玩家...
总算了解!htx矩阵wepok... 1、总算了解!htx矩阵wepoker辅助,epoker透视底牌,必胜教程(有挂辅助);详细教程。2...
发现玩家!hhpoker有没有... 发现玩家!hhpoker有没有作弊挂,agpoker辅助,大神讲解(有挂神器);支持2-10人实时对...
发现玩家!扑克之星辅助,pok... 发现玩家!扑克之星辅助,pokemmo手机脚本,AI教程(有挂辅助);建议优先通过pokemmo手机...
重大来袭!德州局脚本,wpk辅... 重大来袭!德州局脚本,wpk辅助,辅助教程(有挂技巧);1.德州局脚本 ai辅助创建新账号,点击进入...
玩家必备攻略!hhpoker怎... 玩家必备攻略!hhpoker怎么开透视,pokerworld破解版下载,新版2025教程(有挂技巧)...
实测发现!xpoker怎么作弊... 实测发现!xpoker怎么作弊,wepoker数据分析工具,详细教程(有挂方法);原来确实真的有挂(...
推荐攻略!aapoker怎么设... 推荐攻略!aapoker怎么设置提高好牌几率,wepoker有人用过吗,德州教程(有挂教程)1、很好...
研究成果!hhpoker透视脚... 这是一款非常优秀的hhpoker透视脚本视频 ia辅助检测软件,能够让你了解到hhpoker透视脚本...
今日百科!德州局HHpoker... 1、今日百科!德州局HHpoker透视脚本,wepoker透视脚本网页,必备教程(有挂方法);详细教...