11

Is there any way to set focus in input controls using AngularJS/Angular-UI ?

I saw that Angular-UI has some Jq-UI=focus directive but I'm unable to get it to work.

I have a few dialogs that I display using Angular-UI $dialog service and would really like the first input on each dialog to get focus once it's shown

2
  • Can you provide a jsfiddle? Commented Jul 8, 2013 at 9:02
  • You are missing ui-refresh Commented Jul 8, 2013 at 17:52

4 Answers 4

19

I used the standard autofocus attribute in my template, and declared a simple directive to make it work for elements that are created after page load (like dialogs in your case).

app.directive('autofocus', function () {
  return {
    restrict: 'A',
    link: function (scope, element) {
      element[0].focus();
    }
  };
});
Sign up to request clarification or add additional context in comments.

3 Comments

Agreed. Nice simple solution that works well. I just added it to an ngDialog popup and it worked perfectly. Thanks!
Noob here, apologies: What does restrict: 'A' do?
@travis-heeter: it means the directive will be used as an attribute, not an element. See docs.angularjs.org/guide/directive#directive-types
10

Turns out that all you need is this if you use it together with the Angular-ui $dialog service:

app.directive('focusMe', function ($timeout) {    
    return {    
        link: function (scope, element, attrs, model) {                
            $timeout(function () {
                element[0].focus();
            });
        }
    };
});

html:

  <input type="text" focus-me >

as stated, this is for Angular-UI $dialog service only.

See it live here: http://strengthtracker.apphb.com (click login or register to bring up the dialogs)

6 Comments

How to set focus on the footer buttons .. i am really not able to do so .. i need to close $dialog.messageBox if enter key is pressed
add a <form> tag around the controls + button in your dialog
This link appears to be dead.
please Continue this link, even i want to know how to add focus to footer button. Same issue as what Saurabh asked above
fyi also works with $modal service in Angular which is what $dialog was refactored into in later versions of Angular UI
|
0

You are right, I've shown demos on how you can call jQuery's native jQuery.fn.focus() method on a DOM element, as that's all ui-jq really does. The trick is getting it to fire multiple times, and at the right time.

ui-jq already executes the method for you inside a $timeout, however by default it only fires once, when the DOM element is created. In order to make ui-jq fire multiple times, simply add ui-refresh="someExpression" and use an expression that you know will change every single time the modal opens. It's alright if it refires when the modal is closed too, .focus() will do nothing if the DOM element is not visible.

So, for the ui-bootstrap modal, simply put the same expression you're using inside the modal="someExpression" and you should be set.

Comments

-5

The last version of AngularJS offers ng-focus : http://docs.angularjs.org/api/ng/directive/ngFocus

3 Comments

doesnt work for me, the value passed into the directive watch function is just a string, e.g. 'focusInput' not the evaluated value of focusInput.. something must be wrong?
Doesn't work for me either. No errors thrown, simply nothing happens.
That's a callback for when a field is focused on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.