如何将ng-keypress更改为ng-change功能 [英] how to change ng-keypress to ng-change function

查看:75
本文介绍了如何将ng-keypress更改为ng-change功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个文本框。在该字段中有一个验证需要只允许数字,字母和西班牙字符。为此我在javascript中找到了一个函数。该功能在ng-keypress中,我想将其更改为ng-change。如果我改变,该功能不会触发。

Here I have a text box. In that field There is a validation that need to allow only numeric,alphabets and spanish characters. For that I found a function in javascript. That function is in ng-keypress, I want to change it to ng-change. If I change, The function is not firing.

如何将功能更改为ng-change。

How to change the function to ng-change.

JSP代码: -

<input type="text" ng-model="name" ng-keypress="getPatternForAlphebet($event,$index)">

脚本代码: -

 $scope.getPatternForAlphebet = function(event,rowIndex){   
        var key = key || event.which;
        if($scope.documentTypeNumber==1 || $scope.documentTypeNumber ==2){
            if ((key > 64 && key < 91)|| (key > 159 && key < 166) || (key > 96 && key < 123) || (key == 165) ||(key == 32)
                    || (key == 164) || (key == 130) || (key == 181) || (key == 144) || (key == 214) ||
                    (key == 224) ||(key == 233) || (key == 8) || (key == 241) || (key == 209)) {
            }else{
                event.preventDefault();
            }
        }else if($scope.documentTypeNumber==6){
            if ((key > 64 && key < 91)|| (key > 159 && key < 166) || (key > 96 && key < 123) || (key == 165) ||(key == 32) ||
                    (key == 59) || (key == 164) || (key == 58) || (key == 46) || (key == 44) || (key == 38) ||
                    (key == 34) || (key == 130) || (key == 181) || (key == 144) || (key == 214) ||
                    (key == 224) ||(key == 233) || (key == 8) || (key == 241) || (key == 209)) {
            }else{
                event.preventDefault();
            }
        }
    };


推荐答案

我认为您可以使用指令代替。请根据需要找到以下代码更新,希望这会有所帮助。请为Backspace添加密钥代码并在两者中删除。谢谢

I think you can use a directive instead. Please find the following code update it as you need, I hope this will help. Please add the key codes for Backspace and delete in both. Thank you

<input type="text"  ng-model="name" documentvalue="{{documentTypeNumbe}}" filtered-character>

directive('filteredCharacter', function () {
  return function (scope, element, attrs) {

    element.bind("keydown paste", function (event) {
      //console.log($.inArray(event.which,keyCode));
      var keyCode = [];
      if (attrs.documentvalue == 1 || attrs.documentvalue == 2) {
        keyCode = [65, 91, 159]; // Please add all the required key codes
      } else if (attrs.documentvalue == 6) {
        keyCode = [67, 93, 161]; // Please add all the required key codes
      }
      if ($.inArray(event.which, keyCode) === -1) {
        scope.$apply(function () {
          scope.$eval(attrs.filteredCharacter);
          event.preventDefault();
        });
        event.preventDefault();
      }

    });
  };
});

这篇关于如何将ng-keypress更改为ng-change功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆