NG后,显示在输入栏上设置焦点 [英] Setting focus on an input field after ng-show

查看:103
本文介绍了NG后,显示在输入栏上设置焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过控制器可以设置输入字段的焦点,一旦数据(在这种情况下,通过$资源)装?

Is it possible set the focus of an input field via a controller once data has loaded (via $resource in this case)?

直到数据从API返回的,但我不能找到一种方法将焦点设置在输入的输入是通过NG-显示隐藏。我知道输入的NG-型号名称,这意味着我可以通过jQuery做输入名字,但如果可能的话AngularJS方式,我宁愿做。

The input is hidden via ng-show until the data is returned from the API but I can't find a way to set the focus on the input. I know the ng-model name of the input, and the input name which means I could do it via jQuery, but I'd rather do it if possible the AngularJS way.

我已经尝试了很多指令的例子就在这里,但他们都似乎是pre-1.2纳克对焦,依靠从按钮第一NG单击(不能在我的情况发生)或只是不工作。结果
任何提示非常感激地接受。

I've tried a lot of directive examples on here but they all seem to be pre-1.2 ng-focus, rely on a ng-click first from a button (can't happen in my case) or just don't work.
Any tips very gratefully received.

推荐答案

这是一个简单的指令,可能会为你工作。

This is a simple directive that might work for you

<input focus-on="!!myResourceData" />

.directive('focusOn',function($timeout) {
    return {
        restrict : 'A',
        link : function($scope,$element,$attr) {
            $scope.$watch($attr.focusOn,function(_focusVal) {
                $timeout(function() {
                    _focusVal ? $element.focus() :
                        $element.blur();
                });
            });
        }
    }
})

这篇关于NG后,显示在输入栏上设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆