13

Normally ng-model updates bound model each time user pushes the key:

<input type="text" ng-model="entity.value" />

This works great in almost every case.

But I need it to update when onchange event occurs instead when onkeyup/onkeydown event.

In older versions of angular there was a ng-model-instant directive which worked same as ng-model works now (at least for the user - i don't know anything about their implementations). So in older version if I just gave ng-model it was updating the model onchange and when I specified ng-model-instant it was updating the model onkeypup.

Now I need ng-model to use on "change" event of the element. I don't want it to be instant. What's the simplest way of doing this?

EDIT

The input still has to reflect any other changes to the model - if the model will be updated in other place, value of the input should reflect this change.

What I need is to have ng-model directive to work just like it worked in the older versions of angularjs.

Here is an explanation of what I'm trying to do: http://jsfiddle.net/selbh/EPNRd/

Szymon Wygnański
  • 10,642
  • 6
  • 31
  • 44

4 Answers4

20

Here I created onChange directive for you. Demo: http://jsfiddle.net/sunnycpp/TZnj2/52/

app.directive('onChange', function() {    
    return {
        restrict: 'A',
        scope:{'onChange':'=' },
        link: function(scope, elm, attrs) {
            scope.$watch('onChange', function(nVal) { elm.val(nVal); });            
            elm.bind('blur', function() {
                var currentValue = elm.val();
                if( scope.onChange !== currentValue ) {
                    scope.$apply(function() {
                        scope.onChange = currentValue;
                    });
                }
            });
        }
    };        
});
SunnyShah
  • 28,934
  • 30
  • 90
  • 137
4

See also: the AngularJS ngChange directive.

When applied to an <input> the changes occurs after each key press not on the blur event.

http://docs.angularjs.org/api/ng.directive:ngChange

reggoodwin
  • 1,514
  • 13
  • 14
  • OP: "I need it to update when onchange event occurs instead when onkeyup/onkeydown event." – Josh Noe Sep 17 '14 at 05:27
  • 1
    It's also worth noting that `ng-change` does not in fact bind to the `change` event in JavaScript. It actually sets a `$viewValue` change listener within the `ng-model` system. Not the same thing. – Peter M. Elias May 15 '15 at 18:38
2

Angularjs: input[text] ngChange fires while the value is changing : This answer provides a much better solution that allows the custom directive to work with ngModel so you can still use all of the other directives that go along with ngModel.

Also, an even more flexible solution that allows for specifying the event to use (not just blur) and other properties should be built in to angular very soon: https://github.com/angular/angular.js/pull/2129

Community
  • 1
  • 1
wired_in
  • 2,593
  • 3
  • 25
  • 32
1

I'm not sure if there is a better way to do this, but you can achieve this using a custom directive (on any jquery event you want)

<input type="text" ng-model="foo" custom-event="bar" />
<p> {{ bar }} </p>

// create the custom directive

app.directive('customEvent', function() {
    return function(scope, element, attrs) {
        var dest = attrs.customEvent;

        $(element[0]).on('any-jquery-event', function(e) {
            e.preventDefault();

            // on the event, copy the contents of model
            // to the destination variable
            scope[dest] = scope.foo;

            if (!scope.$$phase)
                scope.$apply();
        });
    }
});
Ayush
  • 41,754
  • 51
  • 164
  • 239
  • i dont think this works with recent angularjs. I get an error scope.apply() function doesnt exist. – gerl May 23 '14 at 19:00