【问题标题】:Setting focus by id AngularJS通过 id AngularJS 设置焦点
【发布时间】:2018-08-29 16:54:37
【问题描述】:

单击+ 按钮后,焦点将转移到HTML 中的另一个输入字段。除非用户单击另一个输入字段,否则我想将焦点保持在当前输入字段上。这就是我想将焦点移到单击的输入字段的时候。

<div layout="row" layout-align="center center">
    <md-input-container  class="md-block" flex="50">
        <input required id="circleTimeWordListEditText" type="text" placeholder="Words List" ng-model="circleTime.word"/>

        <div ng-messages="$error">
            <div ng-message="required">This is required.</div>
        </div>
    </md-input-container>
</div>

<div layout="row" layout-align="center center">
    <md-button class="md-raised md-primary" type="submit"
               ng-click="circleTime.onAddButtonClicked()">+
    </md-button>
</div>

目前我正在这样做:

document.getElementById('circleTimeWordListEditText').focus();

我也试过这个,但它给了我 NOSEL 错误。

angular.element('circleTimeWordListEditText').focus();

我也试过这个解决方案:

mainApp.factory('focus', function($timeout, $window) {
    return function(id) {
        // timeout makes sure that it is invoked after any other event has been triggered.
        // e.g. click events that need to run before the focus or
        // inputs elements that are in a disabled state but are enabled when those events
        // are triggered.
        $timeout(function() {
            var element = $window.document.getElementById(id);
            if(element)
                element.focus();
        });
    };
});

mainApp.directive('eventFocus', function(focus) {
    return function(scope, elem, attr) {
        elem.on(attr.eventFocus, function() {
            focus(attr.eventFocusId);
        });

        // Removes bound events in the element itself
        // when the scope is destroyed
        scope.$on('$destroy', function() {
            elem.off(attr.eventFocus);
        });
    };
});


onAddButtonClicked : function(elem) {
            //do stuff

            focus('circleTimeWordListEditText');
            /*var temp = document.getElementById('circleTimeWordListEditText');
            alert(temp);
            temp.focus();
            angular.element('circleTimeWordListEditText');
            angular.element('circleTimeWordListEditText').focus();*/
        }
    },

但它也不起作用。

这个问题的推荐解决方案是什么?

【问题讨论】:

    标签: javascript jquery html angularjs angular-material


    【解决方案1】:

    我觉得你可以使用下面的sn-p。

    var myEl = document.getElementById('circleTimeWordListEditText');
    var angularEl = angular.element(myEl);
    
    angularEl.focus();
    

    【讨论】:

    • 我刚刚测试了它,它在这里工作。您确定单击按钮时正在调用您的函数吗?您的函数在控制器中的外观如何?
    • 是的。我在执行您的代码 sn-p 时打印日志
    【解决方案2】:

    我遇到了同样的问题。这对我有用。我希望这对你也有用!

    function someFunction() 
    {
    var element = angular.element("#id here");
    element.focus()
    }
    

    对你来说,以下可能有效:

    function onAddButtonClicked() 
    {
       var element = angular.element("#circleTimeWordListEditText");
       element.focus()
    }
    

    【讨论】:

    • 请告诉我们为什么问题消失了。谢谢!
    • 对我来说这很有效。我想关注一个特定的元素。因此,我向其中添加了 id,并通过使用 angular.element 获取具有 id 的特定元素并将焦点设置到该元素,问题就解决了。
    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多