https://angular.io/api/forms/FormGroup#controls

按照我的方式:

this.form= this.fb.group({
  id: ['', [Validators.required]],
  name: ['', [Validators.maxLength(500)]],
  child: this.fb.group({
    id: [ '', [Validators.required]],
    name: ['']
  })
});

我想要得到child的有效性,比如this.form.controls.child.controls.valid,而.controls renturn AbstractControl引用这个formgroup api.

Angular 编译错误,错误TS2339:""AbstractControl""类型上不存在属性""Controls"".""

推荐答案

你很接近.请参阅下面的代码示例,或者在我创建的非常简单(也很难看)的StackBlitz上使用它.

StackBlitz Demo

In your template be sure to add your child form group.

<div>
    <form [formGroup]="myForm" (ngSubmit)="send()">
      <input type="text" name="name" formControlName="name">
      <div formGroupName="child">
         <input type="text" name="id" formControlName="id">
         <input type="text" name="name" formControlName="name">
      </div>
      <button class="btn btn-primary">send</button>
    </form>
  </div>

然后在您的组件中,您可以像这样访问字段.

this.myForm['controls'].child['controls'].id.valid

我为这个例子创建的react 式表单:

this.myForm = this.fb.group({
      name: ['', [Validators.maxLength(500)]],
      child: this.fb.group({
        id: ['', [Validators.required]],
        name: ['']
      })
    });

**Update Dec 2019**

我最初的回答有点过时.现在有一个更干净的方法来实现这一点!下面是清洁剂解决方案的示例代码.

this.myForm.get('child.id').valid

Angular相关问答推荐

如何在Angular模块中的forRoot中注入配置

日语字符不受角形约束控制

为什么当我在父组件中设置数组元素时,Angular重新创建子组件而不是更新它?

为什么这个拦截器只在发送事件上触发,而不是在实际响应上触发?

Angular 升级到v16和Ant设计错误:';nzComponentParams';类型';ModalOptions';中不存在

停靠的Angular 应用程序的Nginx反向代理无法加载assets资源

Angular KendoGrid RowReorderable,drop不适用于新添加的行

如何防止打印后的空格后的HTML元素的Angular ?

如何使用Angular 将图像作为二进制数据发送到我的API?

Angular 路由中的模块构建失败

从组件调用时服务中的 AddTodo 方法不起作用

如何禁用特定数据集图表js的默认标签

angular material日期 Select 器中的日期不正确

如何以像素为单位将属性绑定到 style.width?

等待多个promises完成

Angular - 是否有 HostListener-Events 列表?

如何在 Angular 2 的同一个组件中使用多个 ng-content?

ng-template 上的 *ngFor 不输出任何内容

将 [NgStyle] 与条件组合(if..else)

Angular CLI 为已经存在的组件创建 .spec 文件