angular-hotkeys
angular-hotkeys copied to clipboard
What if i want to pass one or more parameters to a function?
+1
+1
@rankill @chrifmarwen @morteza-gho Parameters from where? Normally all you need to know is that a particular key/combo has been pressed. However if there is other data that you know or have access to when registering hotkeys that you want to pass to the function that will ultimately handle the action, then just register the hotkey using a wrapper function that calls the real handler with whatever parameters it requires.
I want to access element, for example I want send $index
to callback
function:
hotkey="{'alt+a': addNewItem($index)}"
hotkeys.bindTo($scope)
.add({
combo: 'alt+a',
description: 'Add new Item',
callback: (event, hostkey, index) => {
$log.log(index);
}
});
@morteza-gho Is that the $index
that Angular provides for you in the ngRepeat
directive? I'm not really sure why you would want to tie a hotkey to specific DOM elements like that; what is it you are trying to do? There may be a better way to achieve it.
@aramando I want to delete a row in a table, so I need $index
@aramando Do you have any idea?
@morteza-gho Take a look at this: https://www.bennadel.com/blog/2450-using-ngcontroller-with-ngrepeat-in-angularjs.htm
It describes how to use the ngController directive to define a controller to be used for each item in an ngRepeat loop. Then you can define a unique hotkey in the dedicated controller for each item in your loop. You'll have access to the array index from $scope.$index, and also the item itself at $scope.item or whatever you called it in the template. This controller can be really simple, it basically just needs to inject the scope, the hotkeys service and a data service, and adds a hotkey function that calls a delete method on the data service and passes it the ID of the data item from the scope.