AngularJS 禁用键的方法
AngularJS way to disable a key
在我的 AngularJs 项目中,我想禁用所有视图的退格键。这是一个简单的 jQuery 解决方案。在AngularJs?
中有什么解决方案吗?
$(document).on("keydown", function (e) {
if (e.which === 8 && !$(e.target).is("input, textarea")) {
e.preventDefault();
}
});
此致
您可以将 ng-keydown
属性添加到您的 <input
> 标签:
<input type="text" ng-keydown="checkKey($event)"/>
$scope.checkKey(keyEvent) {
if (keyEvent.which === 13) {
$event.preventDefault();
}
}
$document.on("keydown", function (e) {
if (e.which === 8 && !$(e.target).is("input, textarea")) {
e.preventDefault();
}
});
$document service has methods which are a subset of jQuery called jqLite。
How can apply this code? My starting app is like 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");
如果你想禁用整个网站的退格键,你可以使用类似的东西()并使用 $document
.[=13 将指令应用于正文文档=]
像这样:
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>
在我的 AngularJs 项目中,我想禁用所有视图的退格键。这是一个简单的 jQuery 解决方案。在AngularJs?
中有什么解决方案吗?$(document).on("keydown", function (e) {
if (e.which === 8 && !$(e.target).is("input, textarea")) {
e.preventDefault();
}
});
此致
您可以将 ng-keydown
属性添加到您的 <input
> 标签:
<input type="text" ng-keydown="checkKey($event)"/>
$scope.checkKey(keyEvent) {
if (keyEvent.which === 13) {
$event.preventDefault();
}
}
$document.on("keydown", function (e) {
if (e.which === 8 && !$(e.target).is("input, textarea")) {
e.preventDefault();
}
});
$document service has methods which are a subset of jQuery called jqLite。
How can apply this code? My starting app is like
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");
如果你想禁用整个网站的退格键,你可以使用类似的东西( 像这样:$document
.[=13 将指令应用于正文文档=]
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>