简体   繁体   English

Angular2输入类型编号,无小数

[英]Angular2 input type number, no decimal value

I've been wondering, what is the proper approach in preventing decimals from being entered in number inputs for angular2. 我一直在想,什么是防止在angular2的数字输入中输入小数的正确方法?

Is there a certain event or constraint I can add? 我可以添加某些事件或约束吗? When I for example just use pattern or such it will only invalidate the field but still allow typing. 例如,当我仅使用模式或类似模式时,它将仅使字段无效,但仍允许键入。

What would be the proper way to prevent this, just ye olde, keyUp event and checking keystroke? 防止这种情况的正确方法是什么,例如老式,keyUp事件和检查击键?

Since according to comments, asking for patterns or w/e is wrong, here is what I now implemented and works like a charm 由于根据评论,要求模式或w / e是错误的,因此这就是我现在实现的方法,它就像一个魅力

<input (keypress)="preventInput($event)" type="number" step="1">

with preventInput as: 与preventInput为:

private preventInput(event) {
  console.log(event.keyCode);
  switch(event.keyCode) {
    case 101: //e
    case 46: //.
      event.preventDefault();
      break;
  }
}

this won't however prevent input of . 但是,这不会阻止的输入。 by pasting but is good enough for us. 通过粘贴,但对我们来说足够好。

I've usually seen it done via custom directives. 我通常看到它是通过自定义指令完成的。 This is preferable to vanilla JS because you can use the scope object to listen into the key events if you want to bind some other angular logic into the event now or in the future. 这比vanilla JS更可取,因为如果您想现在或将来将其他角度逻辑绑定到事件中,则可以使用范围对象侦听键事件。

angular.directive('preventSomeKeystrokes', function () {
    link(scope, elem, attrs) {
       elem.on('keydown', function (event) {
          event.preventDefault();    
          event = event || window.event;
          // maybe prevent certain keys dynamically with $scope?
          // or see how often your users are typing in '.'s with angular.$log().
          var key = _event.keyCode || _event.which;
          key = String.fromCharCode(key);
          var strToTest = '.';
          if (/* some regex with '.' */.test(key)) {
              return false;
          }
      }
});

In angular 2 this is very similar to angular1. 在角度2中,这与角度1非常相似。 The takeaway is that it is preferable to keep the logic running within angular because you can trigger/react to other angular events based on the current event, if you ever needed to do so, or if the logic in this class ever was to grow. 得出的结论是,最好使逻辑在角度范围内运行,因为如果您需要这样做,或者如果此类中的逻辑将会增长,则可以基于当前事件触发/响应其他角度事件。

The syntax is different but the logic is very much the same—creating a directive that listens to keystrokes. 语法不同,但是逻辑非常相同-创建一个监听按键的指令。

@Directive({
   selector: 'prevent-input',
   template: `<input type="text" (keypress)="preventKeyStrokes($event)">`
})

export class PreventInput {
   constructor() {}
   preventKeyStrokes(event) {
          event.preventDefault();    
          event = event || window.event;
          var key = event.keyCode || event.which;
          key = String.fromCharCode(key);
          var strToTest = '.';
          if (/* some regex with '.' */.test(key)) {
              return false;
          }
    } 
}

$event is a DOM Keyboard event. $event是一个DOM键盘事件。 Let me know if I can be of any more help. 让我知道是否能再提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM