【发布时间】:2019-09-19 18:58:51
【问题描述】:
我已经为文本输入编写了一个指令,以支持 int 值。
这里是
import { NgControl } from '@angular/forms';
import { HostListener, Directive } from '@angular/core';
@Directive({
exportAs: 'number-directive',
selector: 'number-directive, [number-directive]'
})
export class NumberDirective {
private el: NgControl;
constructor(ngControl: NgControl) {
this.el = ngControl;
}
// Listen for the input event to also handle copy and paste.
@HostListener('input', ['$event.target.value'])
onInput(value: string) {
// Use NgControl patchValue to prevent the issue on validation
this.el.control.patchValue(value.replace(/[^0-9]/g, ''));
}
}
和 HTML
<div class="form-group">
<label>{{ l("RoomWidth") }}</label>
<input
decimal-number-directive
#roomWidthInput="ngModel"
class="form-control nospinner-input"
type="text"
name="roomWidth"
[(ngModel)]="room.roomWidth"
maxlength="32"
/>
</div>
但我需要它来支持十进制值。例如 99.5
我需要如何修改它?
【问题讨论】:
-
This 正则表达式?
-
你能提供 stacblitz 吗?
-
您是否将
0-9数字替换为空字符串? -
现在我用空字符串替换所有符号并只写数字@PrashantPimpale
-
请提供相关的html和Ts代码
标签: javascript angular typescript angular2-directives