我如何焦点的按钮其启用后? [英] How can I focus a button after its been enabled?

查看:114
本文介绍了我如何焦点的按钮其启用后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML按钮,就像这样:

I have an html button like so:

<按钮自动对焦角色=按钮NG残疾=$的东西无效。> {{}继续}< /按钮>

按钮最初是禁用,因此自动对焦属性不产生任何影响。我希望发生的是,当该按钮并得到允许,我希望把重点放在按钮的时候了。

The button is initially disabled so the autofocus attribute doesn't have any effect. What I want to happen is when the button does get enabled, I want the focus on the button right away.

我怎样才能做到这一点?

How can I accomplish this?

编辑:如下所述,一个指令来监视数值,然后将焦点设置的元素是解决这个的方法之一。我试图创建相同的指令如下,但重点似乎并没有越来越设置。

edit: As stated below, a directive to watch for a value then set focus to the element is one way to solve this. I've tried to create the same directives as below but focus doesn't seem to be getting set.

推荐答案

啊我得到它的工作。我只好将焦点设置函数$超时事件循环,像这样的一个周期后,火了:

Ah I got it working. I had to set the focus function to fire off after one cycle of the event loop with $timeout like so:

.directive('focusToMe', function($timeout) {
        return {
            restrict: 'A',
            compile: function() {
                var directiveName = this.name;

                return function(scope, elem, attrs) {
                    scope.$watch(attrs[directiveName], function(newVal, oldVal) {
                        if (newVal) {
                            $timeout(function() {
                                elem[0].focus();
                            }, 0)
                        }
                    })
                }
            }
        }
    })

这篇关于我如何焦点的按钮其启用后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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