简体   繁体   中英

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 .

<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 . 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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