dir.directive('defaultEnter', [ '$timeout', function($timeout) {
return {
restrict: 'A',
require: '?form',
scope:{
ngDisabled: '=',
},
link: function (scope, element, attr) {
$document.on('keydown', function(evt) {
var which = evt.which;
if (which == 13) {
scope.$apply(function (){
scope.$parent.$eval(attr.ngClick);
});
evt.preventDefault();
evt.stopPropagation();
}
});
}
}
}]);
答案 0 :(得分:0)
通过监听$destroy
事件,每当附加指令的元素被销毁时,删除keydown处理程序。
link: function (scope, element, attr) {
var handler = function(evt) {
var which = evt.which;
if (which == 13) {
scope.$apply(function (){
scope.$parent.$eval(attr.ngClick);
});
evt.preventDefault();
evt.stopPropagation();
}
};
var cleanup = function () {
$document.off('keydown', handler);
};
$document.on('keydown', handler);
element.on('$destroy', cleanup);
}