我想,當Escape鍵被按下寫一個AngularJS指令運行功能:AngularJS按鍵時的鍵碼指令
HTML
<input type="text" custom-keypress="consoleLog()">
JS
var app = angular.module('app', []);
app.controller('appCtrl', function($scope) {
$scope.consoleLog = function() {
console.log('works')
}
});
app.directive('customKeypress', function() {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 27) {
scope.$apply(function(){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
不幸的是,當我按下Escape鍵時沒有任何反應。
任何想法我做錯了什麼?
你有一個錯字有...'ngEnter' V/S'customKeypress'? – PSL
嘗試刷新plunk - 應該是'custom-keypress'和'customKeypress' – Ryan