ATrubka ATrubka - 7 months ago 34
AngularJS Question

Override key navigations in AngularJS input field

I'm writing a custom directive that overrides standard text input element behavior.
Standard input element comes with predefined key navigation behavior.
I'd like to override the behavior.

I register callback like this:

element.on('keyup', function(event) {
// Stopping propagation for the event doesn't help
// because native callback gets invoked
// prior to my custom function.
// event.stopImmediatePropagation();

Particularly, by default ArrowDown key event places caret at the end of the input. Instead, I'd like to do something else upon that key event.

I can do that by returning caret back to where it was at the end of my callback function, but that is less than ideal. Instead, I'd like the native function to have never been called.

I wonder if it's possible to remove that native callback from an input field.


Here's the solution suggested by a friend of mine. It works great:

                element.on("keydown", function(event) {
                    // Do something

Note, that "keyup" call back had to be replaced with "keydown".