pootzko
5/19/2015 - 8:33 AM

Directive used to programmatically auto-focus an input element (through broadcasting a string from JS).

Directive used to programmatically auto-focus an input element (through broadcasting a string from JS).

"use strict";

// Taken from here: http://stackoverflow.com/a/18295416/413785
MyApp.Common.directive("ngFocusOn", function () {
    return function (scope, elem, attr) {
        scope.$on("ngFocusOn", function (e, name) {
            if (name === attr.ngFocusOn) {
                elem[0].focus();
            }
        });
    };
}).factory("FocusOn", function ($rootScope, $timeout) {
    return function (name) {
        $timeout(function () {
            $rootScope.$broadcast("ngFocusOn", name);
        });
    };
});