且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以从formcontrol获取minlength值?

更新时间:2023-11-25 16:24:58

是的,您可以访问长度的数字,包括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