zoukankan      html  css  js  c++  java
  • angular 调用element的 onfocus onkeydown onblur等事件

    项目里要实现一个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('focusDir',[ "$timeout","$parse" ,function($timeout,$parse) {
        return {
            link: function($scope, element, attrs) {
                var model = $parse(attrs.focusDir);
                $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));
                });
            }
        };
    }] );
    
    //Tip:记得 引入"$timeout","$parse" 
    

      

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

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

  • 相关阅读:
    学习!
    第10天:自适应高度
    第9天:第一个CSS布局实例
    nodejs in windows
    网络驱动器无法显示SVN图标问题
    nodejs获取文件修改时间
    gears旅程
    @import和link标签的差别
    evernote诡异bug
    让你的chromium支持支付宝
  • 原文地址:https://www.cnblogs.com/Zoes/p/5021611.html
Copyright © 2011-2022 走看看