2017-03-24 19 views
1

私のAngularJsプロジェクトでは、すべてのビューのバックスペースキーを無効にします。ここには簡単なjQueryソリューションがあります。 AngularJsで書かれたこれに対する解決策はありますか?キーを無効にするAngularJS方法

$(document).on("keydown", function (e) { 
    if (e.which === 8 && !$(e.target).is("input, textarea")) { 
     e.preventDefault(); 
    } 
}); 

よろしく

答えて

1

あなたがバックスペースを無効にしたい場合あなたがこれに似た何かを使うことができるウェブサイト全体のキー(Angularjs : disable tab key default behaviour$documentサービス(@georgeawg)の投稿を使用して本文の本文に指示を適用してください。このような

何か:

angular.module('app', []).directive('muteKey', ['$document', function($document) { 
    return function(scope, element, attrs) { 
    $document.on("keydown", function(e) { 
    /* 
     * you don't need the $(e.target) if you want to 
     * disable the key press in any place of the project 
     * and not just for inputs or textareas 
     */ 
     if (e.which === 8) { 
     e.preventDefault(); 
     } 
    }); 
    }; 
}]); 

<body mute-key> 
    <!----> 
</body> 
1

あなたの<input>タグにng-keydown属性を追加することができます。

<input type="text" ng-keydown="checkKey($event)"/> 

$scope.checkKey(keyEvent) { 
    if (keyEvent.which === 13) { 
     $event.preventDefault(); 
    } 
} 
+0

おかげで、しかし、私はこのコードを適用することができますどのようにウィンドウ全体 –

1

$document servicesubset of jQuery called jqLiteているメソッドを持って$document Service

$document.on("keydown", function (e) { 
    if (e.which === 8 && !$(e.target).is("input, textarea")) { 
     e.preventDefault(); 
    } 
}); 

を使用します。このコードを適用するにはどうすればよい


?私の出発のアプリは、実行ブロックに入れangular.module("my_app", ['ngRoute']);

のようなものです:

angular.module("my_app").run(function($document) { 

    $document.on("keydown", function keydown Handler(e) { 
     //Put handler code here 
    }); 

}); 

!$(e.target).is("input, textarea") 

を翻訳することができます:

(e.target.type == "input") || (e.target.type == "textarea"); 
+0

のキーを無効にしたいですか?私の開始アプリケーションはangular.module( "my_app"、['ngRoute'])のようなものです。 –

+0

'$(e.target)'の部分はどうですか?これはjQueryを使用しないのですか? – lealceldeiro

関連する問題