Angular js trigger blur event

Problem

I'm trying to setup an input, such that when length == 5, it will automatically trigger a blur event. How can I do this?

This is pretty much the concept:

        <input type="tel" placeholder="type here." maxlength="5" name="digits" ng-model="digits" ng-change="if (digits.length ==5) { TRIGGER BLUR};">

Thanks

Problem courtesy of: Federico

Solution

Here is a basic directive that should get you what you are looking for:

app.directive('lengthChecker', function() {
  return function(scope, element, attributes) {
    scope.$watch(attributes.lengthChecker, function(newVal, oldVal) {
      if (newVal.length >= 5) element[0].blur();
    });
  };
});

Html:

<input ng-model="digits" length-checker="digits"/>
Solution courtesy of: Brian Lewis

Discussion

There is currently no discussion for this recipe.

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