我創建了一個指令來監聽向上和向下箭頭的按下。但我想提取被按下的鍵。有沒有辦法讓它通過指令?從Angular指令中獲取參數
/**
* Call a function when down and up arrow keys are pressed
*/
directives.directive('arrowKey', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
//Arrow down
if(event.which === 40) {
scope.$apply(function(){
scope.$eval(attrs.arrowKey);
});
event.preventDefault();
//Arrow Up
}else if (event.which === 38){
scope.$apply(function(){
scope.$eval(attrs.arrowKey);
});
event.preventDefault();
}
});
};
});
HTML用法:
<input id="search" type="text" placeholder="Search" class="searchBox" ng-model = "searchText" ng-change = "getAutoCompleteSuggestions(searchText)" ng-enter="getMore(searchText)" arrow-key="arrowPressed(arg)"/>
爲什麼不使用角度NG-按鍵? – Nix
因爲我不知道....哈哈。謝謝! –