ATrubka ATrubka - 2 months ago 6
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.

Answer

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

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

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

Comments