2
我創建了一個綁定鍵盤快捷鍵的角度指令。但是,一旦綁定,它就會保留所有其他div的綁定。但我只附加到一個div。如何在執行後解除綁定,並在用戶在該div內單擊時重新綁定。 例如:如何在角度js中解除鍵盤快捷鍵事件
angular.module('Dummy').directive('keypressEvents',
function ($document, $rootScope) {
return {
restrict: 'A',
link: function() {
$document.bind('keydown', function (e) {
if ((e.which == '115' || e.which == '83') && (e.ctrlKey || e.metaKey)){
$rootScope.$broadcast('Ctrl+s');
}
});
}
} });
在控制器
$rootScope.$on('Ctrl+s', function (e) {
$scope.$apply(function() {
$scope.doDummyAction();
});
});
在HTML
<div keypress-events>this is a div that binds keyboard shortcut</div>
<div>Another div which doesn't need a short cut key</div>
欣賞任何建議。
太好了。有效。非常感謝。 – user2406718