angular-hotkeys icon indicating copy to clipboard operation
angular-hotkeys copied to clipboard

What if i want to pass one or more parameters to a function?

Open rankill opened this issue 9 years ago • 8 comments

rankill avatar Oct 26 '15 19:10 rankill

+1

marwen-cherif avatar Dec 11 '15 19:12 marwen-cherif

+1

morteza-gho avatar Sep 13 '18 10:09 morteza-gho

@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.

aramando avatar Sep 13 '18 10:09 aramando

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 avatar Sep 13 '18 10:09 morteza-gho

@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 avatar Sep 13 '18 10:09 aramando

@aramando I want to delete a row in a table, so I need $index

morteza-gho avatar Sep 15 '18 03:09 morteza-gho

@aramando Do you have any idea?

morteza-gho avatar Sep 17 '18 07:09 morteza-gho

@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.

aramando avatar Sep 17 '18 08:09 aramando