首页 > 其他 > 详细

angular 调用element的 onfocus onkeydown onblur等事件

时间:2015-12-05 15:57:54      阅读:314      评论:0      收藏:0      [点我收藏+]

项目里要实现一个input验证通过就切换到下一个input的功能

当然用jq dom操作很简单  ,大家都懂,现在用 angular,mvc 数据模型控制分离,不想再dom操作怎么办

以下方法  

<textarea style="height: 73px;min-height: 73px;resize: none "
          type="text"
          focus-me="item.Bake.FocusNext"
          maxlength="500"
          ng-model="item.Bake.PromoText" />

 

 
app.directive(‘focusMe‘, function($timeout, $parse) {
  return {
    //scope: true,   // optionally create a child scope
    link: function(scope, element, attrs) {
      var model = $parse(attrs.focusMe);
      scope.$watch(model, function(value) {
        console.log(‘value=‘,value);
        if(value === true) { 
          $timeout(function() {
            element[0].focus(); 
          });
        }
      });
      // to address @blesh‘s comment, set attribute value to ‘false‘
      // on blur event:
      element.bind(‘blur‘, function() {
         console.log(‘blur‘);
         scope.$apply(model.assign(scope, false));
      });
    }
  };
});

 这个事focus ,类似的 大家可以用于kedown keyup等等 

引用:http://stackoverflow.com/questions/14833326/how-to-set-focus-on-input-field

angular 调用element的 onfocus onkeydown onblur等事件

原文:http://www.cnblogs.com/Zoes/p/5021611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!