Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* global angular */
/**
* https://gist.github.com/mlynch/dd407b93ed288d499778
*
* the HTML5 autofocus property can be finicky when it comes to dynamically
* loaded templates and such with AngularJS. Use this simple directive to tame
* this beast once and for all.
*
* Usage:
* <input type="text" autofocus>
*/
(function () {
'use strict';
angular.module('utils.autofocus', [])
.directive('autofocus', ['$timeout',
function ($timeout) {
return {
restrict: 'A',
link: function ($scope, $element) {
$timeout(function () {
$element[0].focus();
});
}
};
}
]);
})();
@vincentjames501

This comment has been minimized.

Copy link

vincentjames501 commented Mar 11, 2015

Awesome! Works great.

@philipkobernik

This comment has been minimized.

Copy link

philipkobernik commented Aug 3, 2015

This is great, thanks.

@zorzysty

This comment has been minimized.

Copy link

zorzysty commented Aug 17, 2015

Thank you!

@Manouli

This comment has been minimized.

Copy link

Manouli commented Oct 21, 2015

This doesnt seem to be working on modals though.

@sparkidea-codecrab

This comment has been minimized.

Copy link

sparkidea-codecrab commented Mar 18, 2016

Can it be made worked on Modals?

@dodekx

This comment has been minimized.

Copy link

dodekx commented Mar 16, 2017

    return {
        restrict: 'A',
        link: {
            post: function(scope, element) {
                element.ready(
                    function() {
                        $timeout(function() {
                            element[0].focus();
                        })
                    }
                );
            }
        }

    };
}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.