keydown
当您重新访问页面时,指令中的事件会两次绑定。您可以做的是,在离开页面之前,注意删除
键盘按下
指令事件,用于同一位置挂钩
$destroy
事件
scope
directives.directive('keypressEvents', ['$document', '$rootScope', function ($document, $rootScope) {
return {
restrict: 'A',
link: function (scope) {
var event = function (e) {
$rootScope.$broadcast('keypress', e, e.which);
};
$document.on('c', event);
scope.$on('$destroy', function (){
angular.element($document).off('keydown', event);
})
}
}
}]);
笔记
:从jQuery 3.0开始,
.unbind()
.off()
方法,因此它的使用是