如何获取嵌套formBuilder组的值 [英] How do I get the value of a nested formBuilder group

查看:210
本文介绍了如何获取嵌套formBuilder组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的嵌套表单当前以这种方式设置格式:

My nested form is currently formatted in this way:

this.form = this.formBuilder.group({
      user:  this.formBuilder.group({
        id: ['', Validators.required],
        name: ['', Validators.required],
        phone: ['', Validators.required]
      })
})

我通常会这样访问值:

let userID = this.Form.controls['id'].value;
let userName = this.Form.controls['name'].value;
let userPhone = this.Form.controls['phone'].value;

但是因为formGroups是嵌套的,所以我不确定如何访问嵌套的值.我试过了:

but because the formGroups are nested, I'm not sure how to access the nested values. I tried:

let userName = this.Form.controls['user'].name;

访问嵌套formGroup中的表单控件值的正确语法是什么?谢谢

What's the correct syntax for accessing a form control value in a nested formGroup? Thanks

推荐答案

我能够通过执行以下操作来访问该值:

I was able to access the value by doing the following:

let userName = this.Form.controls['user'].value.name;

let userName = this.Form.get(['user','name']).value;

任何一种都可以.

这篇关于如何获取嵌套formBuilder组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆