如何通过控件的formGroupName直接访问控件 [英] How to access the control directly by its formGroupName

查看:27
本文介绍了如何通过控件的formGroupName直接访问控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个表单中,我必须访问 formControlName="last" 的控件以显示它的错误.


    


       
       <input formControlName="last" placeholder="Last name">
      
   </div>
   
   

此代码引发了未定义的错误控件"(粗体格式行).控件可以通过 form['controls'].name['controls'].last.invalid 访问,但是有什么办法可以通过它的 formGroupName 直接访问控件吗?

提前致谢

解决方案

你可以试试下面的代码片段

<span *ngIf="form.get('last').invalid">invalid</span>

In This form I have to access the control of formControlName="last" to show errors of it.

<div [formGroup]="form">
    <div formGroupName="name">
        <input formControlName="first" placeholder="First name">
        <input formControlName="last" placeholder="Last name">
        <span *ngIf="name['controls'].last.invalid">invalid</span>
    </div>
    <input formControlName="email" placeholder="Email">
    <button type="submit">Submit</button>
</div>

This code has thrown an error 'controls' of undefined(Bold Formatted line). control could be accessible by form['controls'].name['controls'].last.invalid , but is there any way I could access the control directly by its formGroupName ?

Thanks in Advance

解决方案

Could you try below snippet

<span *ngIf="form.get('last').invalid">invalid</span>

这篇关于如何通过控件的formGroupName直接访问控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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