1

I'm wanting to control the updates of <input> values to the 'enter' keypress. (I also update when they leave the field - but that's not the problematic part)

There's a number of answers showing how to use:

 '<input ng-model-options="{updateOn : 'change blur'}" [...] />

Which does work in Chrome & Firefox - But not in IE (11, in my testing). (In theory - the change event should cover the enter keypress).

My question: Is there a different set of events I can bind to? I'ave made a Plunker example to show/verify the issue.

I'm unsure if IE simply doesn't fire the change event (when pressing enter), I have the wrong events, or if it's a bug in Angular.

(Note: At the time of writing, this is Angular 1.4.3)

3
  • You could trigger change event manually by tracking keydown event with keycode 13. This can be done in another directive. Commented Aug 24, 2015 at 7:13
  • I'd like to see if it's caused by the browser event implementation (or Angular bug) first. Older answers do show a directive implementation, but point out that was the 'old way' of handling things (e.g.: stackoverflow.com/questions/25534290/…)
    – Overflew
    Commented Aug 24, 2015 at 7:17
  • {updateOn : 'change blur'} will update your model on change and blur events. There is no meaning for enter keypress. So if you want to submit something on enter, then you should do it manually by tracking keypresses. Commented Aug 24, 2015 at 7:20

1 Answer 1

2

You can achieve this behaviour by creating a directive called updateOnEnter and then adding this attribute to your HTML element. This code works in all major browsers.

<input id="search_input" type="text" update-on-enter ng-model="filter.search_terms">

angular.module('app').directive('updateOnEnter', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl) {
            element.bind("keyup", function(ev) {
                if (ev.keyCode == 13) {
                    ctrl.$commitViewValue();
                    scope.$apply(ctrl.$setTouched);
                }
            });
        }
    }
});
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.