Angular响应式表单中嵌套表单的问题。如何在ts文件中声明并在html文件中访问字段。
创始人
2024-10-30 08:00:45
0

在Angular响应式表单中,可以使用FormGroup和FormControl来声明和访问嵌套表单字段。以下是一个示例解决方法:

在ts文件中声明嵌套表单字段:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } 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; // 声明嵌套表单的FormGroup

  constructor() { }

  ngOnInit() {
    this.nestedForm = new FormGroup({
      firstName: new FormControl('', Validators.required), // 在嵌套表单中声明字段
      lastName: new FormControl('', Validators.required),
      address: new FormGroup({
        street: new FormControl('', Validators.required),
        city: new FormControl('', Validators.required),
        state: new FormControl('', Validators.required),
        zip: new FormControl('', Validators.required)
      })
    });
  }

  submitForm() {
    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!

以上代码示例中,我们使用FormGroup和FormControl来声明和访问嵌套表单字段,使用formGroup和formControlName指令在html文件中绑定字段。在submitForm方法中,我们可以通过this.nestedForm.value获得嵌套表单的值。

相关内容

热门资讯

我来教大家!佛手在线是不是有挂... 我来教大家!佛手在线是不是有挂,德州辅助工具到底怎么样,一贯真的是有挂(有挂攻略)1、点击下载安装,...
新手必备!哈糖大菠萝软件下载,... 新手必备!哈糖大菠萝软件下载,we poker游戏下,总是存在有挂(今日头条)1、脚本辅助下载、免费...
终于清楚!wepoker游戏的... 终于清楚!wepoker游戏的安装教程,wpk私人局有透视吗,本来有挂(有挂秘籍)1)辅助插件:进一...
科技通报!红龙poker辅助,... 科技通报!红龙poker辅助,pokemmo脚本辅助下载,原来是真的有挂(有挂详情)该软件可以轻松地...
总算明白!hhpoker作弊实... 总算明白!hhpoker作弊实战视频,wpk德州局透视,竟然存在有挂(有挂规律)1、完成有辅助插件,...
玩家必看科普!aa poker... 玩家必看科普!aa poker辅助,aapoker插件下载,果然真的是有挂(存在有挂)1、公共底牌简...
今日头条!wepoker辅助器... 今日头条!wepoker辅助器安装包定制,aapoker怎么开辅助器,切实是真的有挂(有挂攻略)1、...
总算了解!werplan怎么作... 总算了解!werplan怎么作弊,wepoker破解是真的还是假的,都是是真的有挂(有挂总结)1、模...
如何分辨真伪!pokemmo脚... 如何分辨真伪!pokemmo脚本辅助器下载,智星菠萝辅助,总是真的是有挂(发现有挂)1、任何透视是真...
查到实测辅助!wepoker有... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...