How to let ng-model not update immediately? How to let ng-model not update immediately? angularjs angularjs

How to let ng-model not update immediately?


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} }" />


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.


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

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