You can do something like.
<tr ng-keypress="move($event, $index)"> //do not forget to add track by $index in your ng-repeat </tr>
and then in your controller you can access
$event.keycode
to check if the button has been pressed up or down. @Dan already wrote the logic for switching up and down, he skipped the logic to trigger the switch, which is keyUp and keyDown, so just look at his controller, which:
.controller('someCtrl', function($scope, prodOjectsSerice){ // *COPIED* from @Dan. In order to save time for already defined logic $scope.editProdOjects = prodOjectsSerice.getAll() // just an imagined data fetch // move up $scope.somefunction1 = function(index){ if (index === 0) return; // An item at the top can't be moved higher. var temp = $scope.editProdOjects[index - 1]; $scope.editProdOjects[index - 1] = $scope.editProdOjects[index]; $scope.editProdOjects[index] = temp; } // move down $scope.somefunction = function(index){ if (index === $scope.editProdOjects.length - 1) return; // An item at the bottom can't be moved lower. var temp = $scope.editProdOjects[index + 1]; $scope.editProdOjects[index + 1] = $scope.editProdOjects[index]; $scope.editProdOjects[index] = temp; }; //Trigger to invoke @Dan toggling logic $scope.move = function(event, index){ if(event.keycode===38){ somefunction1(index); } else if(event.keycode==40){ somefunction(index); } }; })
source share