问题描述
因此,我进行了表单验证.我的问题是,例如,我能否获得在创建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值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!