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的子表单组。为了访问子表单组中的

相关内容

热门资讯

透视辅助!红龙poker辅助,... 透视辅助!红龙poker辅助,wpk透视工作室,让我来分享经验(有挂技巧)1、下载好辅助软件之后点击...
wepower有外挂!wepo... wepower有外挂!wepoke辅助挂,(Wepoke)竟然是有挂,wpk教程(有挂插件)1、在w...
微扑克辅助机器人!wpk ai... 微扑克辅助机器人!wpk ai辅助,(WEPoke)其实是真的有挂,必备教程(有挂详情)1、实时微扑...
透视神器!hhpoker怎么开... 透视神器!hhpoker怎么开透视,德普之星透视辅助软件下载,热点推荐(有挂技巧)1、很好的工具软件...
德扑之星猫腻!wepoke辅助... 德扑之星猫腻!wepoke辅助机器人,(wePokE)果然有挂,总结教程(有挂解密)暗藏猫腻,小编详...
透视规律!hhpoker万能辅... 透视规律!hhpoker万能辅助器,aapoker辅助器怎么用,实测必看(有挂透明)1、aapoke...
智星德州菠萝偷偷看牌功能!德扑... 智星德州菠萝偷偷看牌功能!德扑之星作弊,(wepOke)好像是真的有挂,力荐教程(有挂解说)1、智星...
透视了解!德普之星辅助正版,w... 透视了解!德普之星辅助正版,wepoker辅助器怎么用,必看攻略(有挂规律)1、机器人多个强度级别选...
wepoke计算辅助!微扑克辅... wepoke计算辅助!微扑克辅助软件,(wEPoke)好像有挂,介绍教程(有挂解说);1、微扑克辅助...
透视数据!wepoker底牌透... 透视数据!wepoker底牌透视脚本,德普之星app安卓版破解版,一分钟揭秘(有挂技巧)1、许多玩家...