2017-10-11 9 views
0

このanglejs指示文を使用して、数字だけを押すようにユーザーを制限しました。そして間違いなくそれはうまくいく。しかし、私は入力フィールドのデータを削除するキーを削除を使用しています。それは私のために働いていません。誰も入力フィールドのデータを削除するための削除キーを許可する考えがあります。AngularJs指令:削除キーを使用して入力フィールドのデータを削除する方法

私はこれを手伝ってください。どんな助けもありがとう。

ありがとうございます。

app.directive('allowOnlyNumbers', function() { 
return { 
    restrict: 'A', 
    link: function (scope, elm, attrs, ctrl) { 
     elm.on('keydown', function (event) { 
      if (event.which == 64 || event.which == 16) { 
       // to allow numbers 
       return false; 
      } else if (event.which >= 48 && event.which <= 57) { 
       // to allow numbers 
       return true; 
      } else if (event.which >= 96 && event.which <= 105) { 
       // to allow numpad number 
       return true; 
      } else if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1) { 
       // to allow backspace, enter, escape, arrows 
       return true; 
      } else { 
       event.preventDefault(); 
       // to stop others 
       return false; 
      } 
     }); 
    } 
} 

});

+1

削除キーのキーコードは「46」 – pbibergal

答えて

0

ディレクティブに含まれている場合は、これを追加します。

else if (event.which == 46) { 
    // to allow delete 
    return true; 
} 

うまくいくと思います。

関連する問題