简体   繁体   中英

Angular js trigger blur event

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM