[英]Angularjs DELETE <div> when I press the delete keyboard
我創建了一個新指令來刪除當用戶按下delt鍵盤但不起作用時使用的指令。我使用了angularjs指令:ng-keypress
指令:keyboard.js
yemp.planner.app .directive('onKeyEnter', ['$parse', function($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('keydown keypress', function(event) {
if (event.which === 46) {
var attrValue = $parse(attrs.onKeyEnter);
(typeof attrValue === 'function') ? attrValue(scope) : angular.noop();
event.deleteTimelineItem();
console.log("delete");
}
});
scope.$on('$destroy', function() {
element.unbind('keydown keypress')
})
}
};
}]);
<div class="time-lime-item-details" onKeyEnter >
<!-- when the user press delete this div should be deleted -->
</di>
然后將腳本行添加到html文件中:
<script type="text/javascript" src="scripts/directives/keyboard.js"></script>
為了捕獲鍵盤事件,應重點關注元素。 默認情況下,div不能聚焦,但是您可以為其設置tabindex="1"
屬性,以便它可以捕獲鼠標單擊或調用focus()方法時的焦點(根據您的方案)。
此外,在為元素附加指令時,請使用破折號表示法on-key-enter
代替onKeyEnter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.