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

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

问题描述

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

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>

<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>

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

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 ?

预先感谢

推荐答案

@sravanponugoti:我们不能在angular中使用[formGroup]. 尝试此代码

@sravanponugoti: we cannot use [formGroup] with in angular. Try this code

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

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

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