如何通过控件的formGroupName直接访问控件 [英] How to access the control directly by its 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屋!