【问题标题】:Angular6 - Get Nested FormGroup using nameAngular6 - 使用名称获取嵌套的 FormGroup
【发布时间】:2018-09-24 12:50:49
【问题描述】:

我有嵌套表单组

address = new FormGroup({
'com.complex.Address':new FormGroup({
            city: cityControl,
            streetName: streetNameControl,
            houseNumberAddition: houseNumberAdditionControl,
            houseNumber: houseNumberControl,
            postcode: postcodeControl
          })
});

我想找到嵌套的表单组,即“com.complex.Address”。

我已经试过了

this.form.get('address').get('com.complex.Address');

但它总是返回 Null 值。

现在,如果我将嵌套表单组(即“com.complex.Address”)更改为任何其他名称,例如“test”并执行this.form.get('address').get('test');,它实际上会返回我想要的值。

但关键是我不能更改嵌套名称,它会包含一些特殊字符。

如何转义字符并根据需要使用表单组?

【问题讨论】:

    标签: typescript angular6 form-control formgroups


    【解决方案1】:

    查看它使用“.”的 .get() 的角度代码作为分隔符来制作路径数组。

    我看不到任何解决方案可以通过它。无论如何你可以使用:

    (<FormGroup>this.form.get('address')).controls['com.complex.Address']; 
    

    【讨论】:

    • 我已经尝试过了,但它显示错误“'AbstractControl' 类型上不存在属性'控件'。”
    • 好的,请参阅编辑后的答案。这只是一个类型问题。您必须具体说明您确信您将获得一个 FormGroup
    • 是的,它解决了问题,谢谢。
    【解决方案2】:

    这样试试

     NewForm = this.fb.group({
            relationshipId: this.fb.group({
                code: [null, Validators.required]
            })});
    
    this.NewForm.get('relationshipId').value.code  
    this.NewForm.get('relationshipId').get('code').value`
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多