AngularJS update model only on blur


I have a requirement that the model should only be updated when the input is blurred or enter is pressed and error/success message will only be shown after so and not during input. Previously I was using AngularJS 1.2 rc2 and have the following code:


angular.module('app', []).directive('ngModelOnblur', function () {
  return {
      restrict: 'A',
      require: 'ngModel',
      link: function (scope, element, attrs, ngModelCtrl) {
          if (attrs.type === 'radio' || attrs.type === 'checkbox') { return; }
          var update = function () {
              scope.$apply(function () {
'input').off('keydown').off('change').on('focus', function () {
              scope.$apply(function () {
          }).on('blur', update).on('keydown', function (e) {
              if (e.keyCode === 13) {
.controller('Ctrl', ['$scope', function ($scope) {
  $scope.getClass = function (input) {
    if (input.$pristine) { return ''; }
    return input.$invalid ? 'has-error' : 'has-success';


<div ng-controller="Ctrl">
  <form name="form" novalidate>
    <div ng-class="getClass(form.firstname)">
      <input type="text" name="firstname" ng-model="firstname" ng-model-onblur ng-pattern="/^\d{4}$/" />
      <div class="error">error</div>
      <div class="success">success</div>
      $pristine: {{form.firstname.$pristine}}


  .error, .success {
    display: none; 
  .has-error .error, .has-success .success {
    display: block; 

But since I upgraded to RC3, things start to break. Although I removed the event handler for input, change, and keydown, $pristine is still set to false during input and so the message will be shown.

I tried setting terminal: true, priority: -1 for the directive, but it still doesn't work. What am i doing wrong?

Here is the plunker:

Thanks in advance

Problem courtesy of: Lucius


It works for me if you give your directive priority: 1. Setting terminal = true means ng-model is never called, priority -1 is not supported I think.

Just some minor things I found with the directive: when you re-focus the input, the model is resetted (form set to pristine), evne if the model is valid. If you empty the input, after the model has been set, the model is not set to undefined.

Solution courtesy of: Narretz


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.