Angular嵌套表单构建器出现“找不到路径上的控件”错误
创始人
2024-10-29 02:30:36
0

在Angular中,嵌套表单构建器是用来处理复杂的表单结构的强大工具。然而,有时候当使用嵌套表单构建器时,可能会遇到“找不到路径上的控件”错误。这个错误通常发生在尝试访问嵌套表单中的控件时。

下面是一个示例代码,演示了如何使用嵌套表单构建器,并解决可能出现的“找不到路径上的控件”错误:

在组件的.ts文件中:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

@Component({
  selector: 'app-nested-form',
  templateUrl: './nested-form.component.html',
  styleUrls: ['./nested-form.component.css']
})
export class NestedFormComponent implements OnInit {

  nestedForm: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.nestedForm = this.fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      address: this.fb.group({
        street: ['', Validators.required],
        city: ['', Validators.required],
        state: ['', Validators.required],
        zip: ['', Validators.required]
      })
    });
  }

  get firstName() { return this.nestedForm.get('firstName'); }
  get lastName() { return this.nestedForm.get('lastName'); }
  get street() { return this.nestedForm.get('address.street'); }
  get city() { return this.nestedForm.get('address.city'); }
  get state() { return this.nestedForm.get('address.state'); }
  get zip() { return this.nestedForm.get('address.zip'); }

  onSubmit() {
    console.log(this.nestedForm.value);
  }

}

在组件的.html文件中:

First Name is required.
Last Name is required.
Street is required.
City is required.
State is required.
Zip is required.

在这个示例中,我们创建了一个嵌套表单构建器,其中包含了一个名为address的子表单组。为了访问子表单组中的

相关内容

热门资讯

wepoke辅助插件,wepo... wepoke辅助插件,wepoke透视挂靠谱吗,智星德州菠萝偷偷看牌功能1、许多玩家不知道wepok...
wepoke透明挂在哪买,wp... wepoke透明挂在哪买,wpk透明挂,智星德州菠萝辅助工具1、构建自己的微扑克辅助插件;2、选择您...
德扑之星软件透明软件多少钱,w... 德扑之星软件透明软件多少钱,wepoke辅助器,智星菠萝德州辅助1、wepoke辅助器ai辅助优化,...
wepoke软件透明挂价格,w... wepoke软件透明挂价格,wpk黑科技被锤原因,智星德州菠萝怎么看有没有开挂(1)wepoke软件...
wepoke透明挂在哪买,we... wepoke透明挂在哪买,wepoke智能ai,智星德州菠萝有挂吗是一款可以让一直输的玩家,快速成为...
智星德州菠萝开挂,wepoke... 一、wepoke辅助挂简介了解软件请加微:136704302wepoke辅助挂是一款在线扑克游戏平台...
aapokerai辅助,wep... aapokerai辅助,wepoke软件透明演示,智星菠萝德州辅助1、许多玩家不知道wepoke软件...
wpk透视辅助测试,wepok... wpk透视辅助测试,wepoke辅助使用教程,智星德州菠萝辅助;wpk透视辅助测试,wepoke辅助...
智星德州菠萝辅助,wepoke... 智星德州菠萝辅助,wepokeai代打逻辑,智星德州菠萝开挂;小薇(透视辅助)致您一封信;亲爱wep...
微扑克辅助透视技能教程,wep... 您好,wepoke透明挂是真的吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...