本文介绍了是否可以从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>

演示

DEMO

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

09-26 06:53