简体   繁体   English

Angular 反应式 forms - 获得最大长度?

[英]Angular reactive forms - get maxlength?

I'm using angular reactive forms for a component library, and there are some requirements for the project that would be made much easier if I could just get the associated maxlength validator (if one exists) inside a form control (it's supposed to show how many characters are left when the EU is typing)我正在将 angular 反应式 forms 用于组件库,如果我可以在表单控件中获取相关的 maxlength 验证器(如果存在),那么项目的一些要求会变得容易得多(它应该显示如何欧盟打字时会留下很多字符)

Specifically, I want to do it from within the component , based on whatever maxlength validator was used, as this is a generic branded component library for use within my company.具体来说,我想根据使用的任何 maxlength 验证器从组件内部执行此操作,因为这是在我的公司内使用的通用品牌组件库。

As far as I can tell, the only way to do this is to pass in the maxlength separately from the validators - but figured I'd ask The Internets to see if there's a cooler way.据我所知,这样做的唯一方法是与验证器分开传递 maxlength - 但我想我会问互联网看看是否有更酷的方法。

I've implemented (and modernized for angular 12) the ControlValueAccessorConnector solve from https://medium.com/angular-in-depth/dont-reinvent-the-wheel-when-implementing-controlvalueaccessor-a0ed4ad0fafd - so I have access to all those properties.我已经实现(并针对 angular 12 进行了现代化改造)从https://medium.com/angular-in-depth/dont-reinvent-the-wheel-when-implementing-controlvalueaccessor-a0ed4ad0fa解决的 ControlValueAccessorConnector 所以我可以访问所有这些属性。

Any help - or a validation that what I'm looking for is not gonna happen - would be appreciated!任何帮助 - 或验证我正在寻找的东西不会发生 - 将不胜感激!

thre're not a "relation" between a FormControl and a input (a FormControl is independient of the input), so you can not access to the htmlElement input if you not use one around-work: a directive FormControl 和输入之间没有“关系”(FormControl 独立于输入),因此如果您不使用一种替代方法,则无法访问 htmlElement 输入:指令

If the selector of one directive is '[formControl], [formControlName]' it is applied to all the inputs with formControl and FormControlName.如果一个指令的选择器是'[formControl], [formControlName]' ,它将应用于所有带有 formControl 和 FormControlName 的输入。

We inject in constructor the NgControl and the ElementRef我们在构造函数中注入 NgControl 和 ElementRef

constructor(private control:NgControl,private el:ElementRef) { }

So we can access to the function validator of the control using this.control.control.validator .因此我们可以使用this.control.control.validator访问控件的 function 验证器。 See that in this way we access to the "function".看到这样我们就访问到了“函数”。 And as all the function we can feed with a FormControl and get the result.和所有 function 一样,我们可以使用 FormControl 输入并获得结果。 In this case we are feeding with a FormControl very large在这种情况下,我们使用一个非常大的 FormControl

The directive can be some like该指令可能类似于

@Directive({
  selector: ''[formControl], [formControlName]''
})
export class AddMaxLengthDirective implements OnInit{

  constructor(private control:NgControl,private el:ElementRef) { }
  ngOnInit()
  {
    const err=this.control.control.validator(
            new FormControl(".".repeat(10000000)))

    if (err && err.maxlength && err.maxlength.requiredLength)
         this.el.nativeElement.setAttribute(
                 'maxlength',err.maxlength.requiredLength)

  }

}

You can see in this stackblitz .你可以在这个 stackblitz中看到。 If you use F12, you can inspect the DOM and see how is added the maxlength attribute如果您使用 F12,您可以检查 DOM 并查看如何添加 maxlength 属性

NOTE: don't forget add the directive in your module!注意:不要忘记在你的模块中添加指令!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM