简体   繁体   English

angularjs - 单击按钮时保持对输入的关注

[英]angularjs - keep focus on input when clicked on a button

I'm trying to keep the focus on the textarea when click on one div a button . 当我点击一个div按钮时,我正在努力将注意力集中在textarea上。

<input type="text" ng-model="myText" id = "area1" ></input>

And my buttons are dynamically generated by template in a directive as, 我的按钮是由模板在指令中动态生成的,

 <my-buttons > </my-buttons>

app.directive('myButtons', function () {
    return {
        restrict: "E",
        templateUrl: "templates/buttons.html"
        link: function ($scope) {}
    };
});

Template is 模板是

<div>
    <div>
        <div ng-click="submitResult();">
            <div ng-style="color:red"> <span>OK</span>
            </div>
        </div>
    </div>
</div>

when I click on submit it removes the focus from the input but I want that the focus to remain on the input where it was. 当我点击提交时,它会从输入中删除焦点,但我希望焦点保持在输入的位置。 I know that we can set the focus by 我知道我们可以设定焦点

document.getElementById("id").focus();

Do I have to add a attribute to the myButtons directive and pass the id of the input or there is any other workaround in angularjs . 我是否必须向myButtons指令添加属性并传递输入的id,或者在angularjs中还有其他任何解决方法。 There can be many number of such input and buttons on the page. 页面上可以有许多这样的输入和按钮。

There is no angular method to tell an element not to loose focus even when another control needs focus. 即使另一个控件需要聚焦,也没有角度方法来告诉元素不要失焦。 You need to implement this functionality yourself by manually handling blur event. 您需要通过手动处理blur事件来自己实现此功能。

Following Charlie's answer, and as a suggestion to get what you want done, try keeping track of who's currently focused. 按照查理的回答,并作为获得你想做的事情的建议,试着跟踪目前关注的人。 On button click, return the focus to the element that was last focused. 单击按钮时,将焦点返回到上次聚焦的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM