Change focus to input on a key event in AngularJS

后端 未结 3 1576
有刺的猬
有刺的猬 2021-02-03 11:49

Test case: http://jsbin.com/ahugeg/4/edit (Slightly long)

In the above test case, I have three input elements, generated by ng-repeat directive. My intentio

3条回答
  •  臣服心动
    2021-02-03 12:43

    I had a similar problem and used this simple directive. It works as ng-show and ng-hide would- only with focus, if it's attribute resolves as true:

    .directive('focusOn',function() {
        return {
            restrict : 'A', 
            link : function($scope,$element,$attr) {
                $scope.$watch($attr.focusOn,function(focusVal) {
                    if(focusVal === true) {
                        setTimeout(function() {
                            $element.focus();
                        },50);
                    }
                });
            }
        }
    })
    

提交回复
热议问题