chieffancypants / angular-hotkeys

Configuration-centric keyboard shortcuts for your Angular apps.
http://chieffancypants.github.io/angular-hotkeys/
MIT License
1.68k stars 248 forks source link

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

Open rankill opened 8 years ago

marwen-cherif commented 8 years ago

+1

morteza-gho commented 6 years ago

+1

aramando commented 6 years ago

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

morteza-gho commented 6 years ago

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);
    }
});
aramando commented 6 years ago

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

morteza-gho commented 6 years ago

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

morteza-gho commented 6 years ago

@aramando Do you have any idea?

aramando commented 6 years ago

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