什么是AngularJS的方式来创建全局键盘快捷方式? [英] What is AngularJS way to create global keyboard shortcuts?
本文介绍了什么是AngularJS的方式来创建全局键盘快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想,我应该使用的指令,但它似乎奇怪指令添加到身体,但监听文件事件。
I suppose that I should use directive, but it seems strange to add directive to body, but listen events on document.
什么是做一个适当的方式?
What is a proper way to do this?
更新:发现AngularJS UI,看到他们实现关键preSS指令。
UPDATE: Found AngularJS UI and saw their realization of keypress directive.
推荐答案
下面是我如何使用jQuery做到了这一点 - 我想有一个更好的办法
Here's how I've done this with jQuery - I think there's a better way.
var app = angular.module('angularjs-starter', []);
app.directive('shortcut', function() {
return {
restrict: 'E',
replace: true,
scope: true,
link: function postLink(scope, iElement, iAttrs){
jQuery(document).on('keypress', function(e){
scope.$apply(scope.keyPressed(e));
});
}
};
});
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.keyCode = "";
$scope.keyPressed = function(e) {
$scope.keyCode = e.which;
};
});
<body ng-controller="MainCtrl">
<shortcut></shortcut>
<h1>View keys pressed</h1>
{{keyCode}}
</body>
这篇关于什么是AngularJS的方式来创建全局键盘快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文