在 ng-show 之后将焦点设置在输入字段上 [英] Setting focus on an input field after ng-show

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

问题描述

数据加载后是否可以通过控制器设置输入字段的焦点(在这种情况下通过 $resource)?

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

输入通过 ng-show 隐藏,直到数据从 API 返回,但我找不到将焦点设置在输入上的方法.我知道输入的 ng-model 名称和输入名称,这意味着我可以通过 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.

我在这里尝试了很多指令示例,但它们似乎都是 1.2 之前的 ng-focus,首先依赖于按钮的 ng-click(在我的情况下不可能发生)或者只是不'没用.
非常感谢收到任何提示.

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[0].focus() :
                        $element[0].blur();
                });
            });
        }
    }
})

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

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