ここにはa sample angular directive to prevent typing non-numeric keys (StackOverflow answer)です。 私はthis fiddleのようなものをいくつかの入力でis-numberディレクティブを使うよう書いています。私の入力にはさまざまなディレクティブがあるので、上の答えのアップデートで提案されているものと同じテンプレートを使用することはできません。角の付いたディレクティブ:数字だけを許可する
var $scope;
var app = angular.module('myapp', []);
app.controller('Ctrl', function($scope) {
$scope.myNnumber1 = 1;
$scope.myNnumber2 = 1;
});
app.directive('isNumber', function() {
return {
require: 'ngModel',
link: function (scope, element) {
scope.$watch(element.ngModel, function(newValue,oldValue) {
newValue = String(newValue);
newValue = newValue.replace('۰', '0').replace('۱', '1').replace('۲', '2').replace('۳', '3').replace('۴', '4').replace('۵', '5').replace('۶', '6').replace('۷', '7').replace('۸', '8').replace('۹', '9');
var arr = String(newValue).split("");
if (arr.length === 0) return;
if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.')) return;
if (arr.length === 2 && newValue === '-.') return;
if (isNaN(newValue)) {
element.ngModel = oldValue;
}
});
}
};
更新: 私は上の非英語番号とを変換するために、いくつかのプロセスを実行する必要があることをご検討ください。 Angular_10の答えに基づいてa new fiddle hereを作成しました。さて、すべてのことは、カーソルの位置を除いて、ペルシャ数字をタイプする間は問題ありません。私がペルシア語の数字を入力すると、それは英語の同等の数字に置き換えられますが、カーソルは突然最後に飛びます。
の詳細あなたが達成したいものを詳細にあなたの質問を詳しく説明してくださいと何をしようとしています。 –
@ Angular_10 [jsとhtml](http://jsfiddle.net/AMShafiee/u1w9v2m7/)のコードで、数字以外のキーを入力しないように指示を書いたコードは機能しません。 [the fiddle](http://jsfiddle.net/AMShafiee/u1w9v2m7/)をチェックし、どうすれば修正できるか教えてください。 – Ahmad