如何通过其formGroupName直接访问控件 [英] How to access the control directly by its 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</span>
</div>
<input formControlName="email" placeholder="Email">
<button type="submit">Submit</button>
</form>
这篇关于如何通过其formGroupName直接访问控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!