Javascript AngularJS禁用密钥的方法
在我的AngularJs项目中,我想对所有视图禁用退格键。下面是一个简单的jQuery解决方案。有没有用AngularJs编写的解决方案Javascript AngularJS禁用密钥的方法,javascript,angularjs,keyboard,backspace,Javascript,Angularjs,Keyboard,Backspace,在我的AngularJs项目中,我想对所有视图禁用退格键。下面是一个简单的jQuery解决方案。有没有用AngularJs编写的解决方案 $(document).on("keydown", function (e) { if (e.which === 8 && !$(e.target).is("input, textarea")) { e.preventDefault(); } }); 关于您可以向标签添加ng keydown属性: $scop
$(document).on("keydown", function (e) {
if (e.which === 8 && !$(e.target).is("input, textarea")) {
e.preventDefault();
}
});
关于您可以向
标签添加ng keydown
属性:
$scope.checkKey(keyEvent){
如果(keyEvent.which==13){
$event.preventDefault();
}
}
使用
这些方法是一种非常有用的方法
如何应用此代码?我的起始应用程序类似于angular.module(“我的应用程序”['ngRoute'])代码>
将其放在运行块中:
angular.module("my_app").run(function($document) {
$document.on("keydown", function keydown Handler(e) {
//Put handler code here
});
});
可以翻译为:
(e.target.type == "input") || (e.target.type == "textarea");
如果要禁用整个网站的backspace键,可以使用类似的方法()并使用$document
将指令应用于正文文档
大概是这样的:
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>
angular.module('app',[])指令('muteKey',['$document',函数($document){
返回函数(范围、元素、属性){
$document.on(“键控”,功能(e){
/*
*如果愿意,您不需要$(e.target)
*在项目的任何位置禁用按键
*而不仅仅是输入或文本区域
*/
如果(e.which==8){
e、 预防默认值();
}
});
};
}]);
谢谢,但我想禁用整个窗口的键。如何应用此代码?我的起始应用程序就像angular.module(“我的应用程序”['ngRoute']);
!$(e.target).is("input, textarea")
(e.target.type == "input") || (e.target.type == "textarea");
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>