我在“计算器”页面上有一个要求,其中有40个字段可用。每个字段均以数字格式(逗号分隔值)验证。字段中不接受字符。我在检查用户输入的数字和逗号的onkeyup事件时遇到问题,我需要在keyup事件中显示一条消息“确定-按TAB键重新计算。”。为此,我创建了指令。它验证但无法在组件上显示消息。我们可以再添加一个选择器吗?或其他解决方案?
<input name="salesprice" type="text" OnlyNumber="true" [ngModel]="salesprice | numberFormat" (ngModelChange)="salesprice=removeCommas($event)" (blur)="onZipChange()" (focus)="calClick($event)" class="input-highlight">
================================================ ========
import { Directive, ElementRef, HostListener, Input, Output, EventEmitter } from '@angular/core';
@Directive({
selector: '[OnlyNumber],[tabText]'
})
export class OnlyNumber {
regexStr = '^[0-9]*$';
//regexStr = ".*[^0-9].*";
constructor(private el: ElementRef) { }
@Input() OnlyNumber: boolean;
@Output() tabText: boolean;
@Output() itch:EventEmitter<any> = new EventEmitter();
/// our click is a representation of a scratch
@HostListener('onClick') onClick(){
///We are emitting itchies!!
/* this.itch.emit('itch itch itch');
console.log("this.itch");
console.log(this.itch); */
}
@HostListener('keydown', ['$event']) onKeyDown(event) {
let e = <KeyboardEvent>event;
let keyCode = e.keyCode;
if (this.OnlyNumber) {
if(keyCode >= 96 && keyCode <= 105) {
keyCode -= 48;
}
if ([46, 8, 9, 27, 13, 110, 190].indexOf(keyCode) !== -1 ||
// Allow: Ctrl+A
(keyCode == 65 && e.ctrlKey === true) ||
// Allow: Ctrl+C
(keyCode == 67 && e.ctrlKey === true) ||
// Allow: Ctrl+V
(keyCode == 86 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(keyCode == 88 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(keyCode >= 35 && keyCode <= 39)) {
return;
}
let ch = String.fromCharCode(keyCode);
let regEx = new RegExp(this.regexStr);
if (regEx.test(ch)){
this.tabText = true;
return;
} else {
e.preventDefault();
}
}
}
@HostListener('blur', ['$event']) onBlur(event) {
let e = <KeyboardEvent>event;
if (this.OnlyNumber) {
/* console.log("this.OnlyNumber");
console.log(event.target.value); */
this.tabText = false;
}
}
}
答案 0 :(得分:0)
尝试这个Solution
directive.ts
import { Directive, ElementRef, HostListener, Input, Output, EventEmitter } from '@angular/core';
@Directive({
selector: '[OnlyNumber],[tabText]'
})
export class OnlyNumber {
regexStr = '^[0-9]*$';
//regexStr = ".*[^0-9].*";
constructor(private el: ElementRef) { }
@Input() OnlyNumber: boolean;
@Output() tabText: boolean;
@Output() itch: EventEmitter<any> = new EventEmitter();
@Output() warning: EventEmitter<any> = new EventEmitter();
/// our click is a representation of a scratch
@HostListener('onClick') onClick() {
///We are emitting itchies!!
/* this.itch.emit('itch itch itch');
console.log("this.itch");
console.log(this.itch); */
}
@HostListener('keydown', ['$event']) onKeyDown(event) {
let e = <KeyboardEvent>event;
let keyCode = e.keyCode;
if (this.OnlyNumber) {
if (keyCode >= 96 && keyCode <= 105) {
keyCode -= 48;
}
if ([46, 8, 9, 27, 13, 110, 190].indexOf(keyCode) !== -1 ||
// Allow: Ctrl+A
(keyCode == 65 && e.ctrlKey === true) ||
// Allow: Ctrl+C
(keyCode == 67 && e.ctrlKey === true) ||
// Allow: Ctrl+V
(keyCode == 86 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(keyCode == 88 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(keyCode >= 35 && keyCode <= 39)) {
return;
}
let ch = String.fromCharCode(keyCode);
let regEx = new RegExp(this.regexStr);
if (regEx.test(ch)) {
this.tabText = true;
return;
} else {
this.warning.emit(false)
e.preventDefault();
}
} else {
this.warning.emit(false);
}
}
@HostListener('blur', ['$event']) onBlur(event) {
let e = <KeyboardEvent>event;
if (this.OnlyNumber) {
/* console.log("this.OnlyNumber");
console.log(event.target.value); */
this.tabText = false;
}
}
}
app.component.html:
<input (warning)="errorOutput($event)" name="salesprice" placeholder="Enter Number" type="text" OnlyNumber="true"
class="input-highlight">
app.component.ts:
import { Component, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() { }
ngOnInit() {
}
errorOutput(event){
if(!event){
alert("Please Enter Number Only");
}
}
}