我创建了一个指令来听取箭头上下按下。但我想提取被按下的键。有没有办法通过指令传递它?
/**
* 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)"/>
答案 0 :(得分:2)
您应该使用ng-keypress。
<input id="search" type="text" ng-keypress='keypress($event)' />
/* ctrl */
$scope.keypress = function($event){
//check event here.
if($event.keyCode == 38){
}
}