[英]Angular : Is there a way to identify html form controls uniquely inside ts component?
Typescript Method打字稿方法
addOneDocument(supplierForm){
const controls = supplierForm.controls;
for(const name in controls){
if (controls[name].invalid) {
controls[name].touched=true;
}
}
}
I want to change this method to check a particular form control for errors.我想更改此方法以检查特定表单控件是否存在错误。 I actually want something like following
我实际上想要像下面这样的东西
if ((controls[name].invalid)&&(controls[name].name=="document"))
but name property doesn't exist on type FormControl.但名称属性在类型 FormControl 上不存在。 How to identify a particular html form control uniquely in typescript?
如何在打字稿中唯一标识特定的 html 表单控件?
You could think of form controls as a tree-like structure.您可以将表单控件视为树状结构。 For example, the parent would be a
FormGroup
and its descendants could be some FormControls
/ FormGroups
/ FormArrays
and so on.例如,父级是一个
FormGroup
,它的后代可能是一些FormControls
/ FormGroups
/ FormArrays
等等。
I'm not aware of any way to identify a form control like this, but the best solution that comes to mind it to use the get()
method, to which you can provide a path to your form control.我不知道有什么方法可以识别这样的表单控件,但想到它的最佳解决方案是使用
get()
方法,您可以通过该方法提供表单控件的路径。
Excerpt from A thorough exploration of Angular Forms : Angular Forms 的彻底探索摘录:
const fg = new FormGroup({
name: new FormControl(''),
address: new FormGroup({
city: new FormControl(''),
street: new FormControl(''),
}),
});
You could use the get
method like this:你可以像这样使用
get
方法:
fg.get('address.city')
// Or
fg.get(['address', 'street'])
So, I'd say that a way to identify a form control would be to use its path .所以,我想说一种识别表单控件的方法是使用它的path 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.