是否可以从 formcontrol 获取 minlength 值? [英] Is it possible to get minlength value from formcontrol?

查看:12
本文介绍了是否可以从 formcontrol 获取 minlength 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有我的表单验证.我的问题是我可以得到例如我在创建 formControl 时传递的 minlength 值吗?

So i have my form validation. And my question is can i get for example a minlength value which i passed at creating formControl?

我没有找到任何信息.

这是我的愚蠢组件,我想获取 minlength 值以传递给信息.现在我需要通过@Input() 传递它.

This is my dumb component and i want to get minlength value to pass to information. Now i need to pass it via @Input().

title: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(20)]]

.

<div class="validation-window">
  <p *ngIf="errors.required">
    {{field}} required
  </p>
  <p *ngIf="formControl.hasError('minlength')">
      {{field}} at least {{minLegth}} characters
  </p>
  <p *ngIf="formControl.hasError('maxlength')">
      {{field}} at least {{maxLength}} characters
  </p>
</div>

我想将 {{maxLength}} 替换为 formControl.validators.minlength.value;

I want to replace {{maxLength}} with something like formControl.validators.minlength.value;

推荐答案

是的,你可以访问长度的个数,maxlength 和 minlength 都可以.您可以在 error 对象中找到它,它位于 .maxlength.requiredLength &&minlength.requiredLength.还有一个带有 actualLength 的字段,但你似乎不需要它,但如果你有时需要它!:)

Yes, you can access the number of the length, both in maxlength and minlength. You can find it inside the error object, where it lies under .maxlength.requiredLength && minlength.requiredLength. There is also a field with actualLength, but you don't seem to need it, but if you sometimes do! :)

<p *ngIf="formControl.hasError('maxlength')">
  {{field}} at max {{formControl.errors.maxlength.requiredLength}} characters
</p>

<p *ngIf="formControl.hasError('minlength')">
  {{field}} at least {{formControl.errors.minlength.requiredLength}} characters
</p>

演示强>

这篇关于是否可以从 formcontrol 获取 minlength 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆