22

Code:

<input type="text" ng-modal="name" />
{{name}}

When I input something into the input, the following {{name}} will change immediately. Is it able to configure it only update the name after I input all characters and leave the input?

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • 1
    possible duplicate of [Angularjs: input\[text\] ngChange fires while the value is changing](http://stackoverflow.com/questions/11868393/angularjs-inputtext-ngchange-fires-while-the-value-is-changing) – Mark Rajcok Feb 06 '13 at 15:18

4 Answers4

53

This is about recent additions to AngularJS, to serve as future answer.

Angular newer versions (now in 1.3 beta), AngularJS natively supports this option, using ngModelOptions, like

ng-model-options="{ updateOn: 'default blur', debounce: { default: 500, blur: 0 } }"

NgModelOptions docs

Example:

<input type="text" name="username"
       ng-model="user.name"
       ng-model-options="{updateOn: 'default blur', debounce: {default: 500, blur: 0} }" />
manikanta
  • 8,100
  • 5
  • 59
  • 66
22

Update

As many have mentioned Angular now has built-in support for this using the ng-model-options directive. See more here.

<input type="text" ng-model="name" ng-model-options="{updateOn: 'blur'}" />

Old answer below:

There's no built-in option for ng-model to change that behaviour, but you can write a custom directive doing it. @Gloopy wrote a directive like that for another question. You can look at the fiddle here.

The directive unregisters from the input and keydown events which trigger the update after each keystroke.

<input type="text" ng-model="name" ng-model-onblur />

Update:

Updated fiddle to use latest stable AngularJS (1.2.16 as of writing), instead of directly referencing the master version at github.

Also added an explicit priority so that the directive is run after ng-model to ensure event listeners are changed correctly.

Community
  • 1
  • 1
Martin
  • 8,876
  • 2
  • 35
  • 36
9

A better option is to use the ng-model-options:

<input type="text" ng-model="name" ng-model-options="{updateOn: 'blur'}" />
Niels Steenbeek
  • 4,692
  • 2
  • 41
  • 50
  • this, however, being the 'right' way only works in newer versions of angular that no longer support ie8... kind of a killer in a lot of dev env :/ – O'Mutt Nov 13 '14 at 14:12
  • 6
    You should simply kill IE8. Google dropped support in 2012. – s.Daniel Dec 09 '14 at 08:48
  • I have the same problem with two fields (user and password) in a form, I want to update when click on login button not on blur or typing characters, my problems is that each time that ng-model update the iphone scroll the page to the top – xzegga Oct 20 '15 at 01:43
  • @s.Daniel +1. If the world keeps supporting IEs from the past, the world might end up using W95 – KhoPhi Jun 16 '16 at 12:20
5

Working directive code(ng-1.2RC3): use: ng-model-onblur

.directive('ngModelOnblur', function () {
  return {
      restrict: 'A',
      require: 'ngModel',
      priority: 1,
      link: function (scope, element, attrs, ngModelCtrl) {
          if (attrs.type === 'radio' || attrs.type === 'checkbox') { return; }
          var update = function () {
              scope.$apply(function () {
                  ngModelCtrl.$setViewValue(element.val().trim());
                  ngModelCtrl.$render();
              });
          };
          element.off('input').off('keydown').off('change').on('focus', function () {
              scope.$apply(function () {
                  ngModelCtrl.$setPristine();
              });
          }).on('blur', update).on('keydown', function (e) {
              if (e.keyCode === 13) {
                  update();
              }
          });
      }
  };
})
mayankcpdixit
  • 2,456
  • 22
  • 23