简体   繁体   English

Angular:有没有办法在 ts 组件中唯一地识别 html 表单控件?

[英]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.

相关问题 如何在Angular中唯一地标识组件的父组件 - How to identify parent component of a component uniquely in Angular 如何在角组件TS中调用组件 - How to call a component inside component TS in angular Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file? 在这种情况下唯一识别用户的最佳方法是什么? - Best way to uniquely identify a user in this case? 如何唯一标识没有 ID 的 HTML 元素? - How to Uniquely Identify HTML Elements without IDs? 组件内的Angular2 Html - Angular2 Html inside a Component Angular 2 获取 .length 但只计算 html 或 ts 组件中的特定数据 - Angular 2 get .length but count only specific data in the html or ts component 有没有一种方法可以唯一标识内容脚本在我的Chrome扩展程序中运行的iframe? - Is there a way to uniquely identify an iframe that the content script runs in for my Chrome extension? angular 形式值 {} 在 ts 文件中是 null 但在 html 中有效 - angular form value {} is null in ts file but works in html 如何唯一标识循环中生成的列表元素(li)的HTML内容? - How to uniquely identify HTML contents of list elements(li) that is generated in a loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM