首页 > 解决方案 > 角度反应形式模式不起作用

问题描述

我在 Angular 上使用 Reactive Form 可以同时使用数字和逗号。我制作了输入类型文本,以便点和逗号出现在移动视图的键盘上。我希望输入的输入只接受数字,但我输入的模式没有帮助。这样做后我仍然可以输入字符,但我不希望这种情况发生。

textInput: new FormControl('', 
[
  Validators.required,
  Validators.pattern('[0-9]{7}')
]),

<input autoComplete="off" autoCorrect="off"
    type="text"
    lang="en"
    [formControl]="this.config.textInput"
    placeholder="0.0"
    minLength={1}
    maxLength={79}
    spellCheck="false"
    (ngModelChange)="amountChanged($event)"
    enterkeyhint="next"
    step="1"
    class="w-100"
>

如何使类型为文本并且只接受数字?

标签: angulartypescriptangular-reactive-forms

解决方案


您可以使用输入类型:数字 ,例如:

<input autoComplete="off" autoCorrect="off"
type="number"
lang="en"
[formControl]="this.config.textInput"
placeholder="0.0"
minLength={1}
maxLength={79}
spellCheck="false"
(ngModelChange)="amountChanged($event)"
enterkeyhint="next"
step="1"
class="w-100">

推荐阅读