javascript Angular js 触发模糊事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23300329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:41:27  来源:igfitidea点击:

Angular js trigger blur event

javascriptangularjseventsangularjs-directive

提问by Federico

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

我正在尝试设置一个输入,这样当长度 == 5 时,它会自动触发模糊事件。我怎样才能做到这一点?

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

谢谢

采纳答案by Brian Lewis

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