Created
October 1, 2015 14:21
-
-
Save matthewbednarski/d3a580c78b873779b931 to your computer and use it in GitHub Desktop.
An angular directive/service for giving focus to elements with a tabIndex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var app = angular.module('mcbFocus', []) | |
.directive('focusOn', function() { | |
return function(scope, elem, attr) { | |
if(attr.tabIndex === undefined){ | |
elem.attr('tabindex', -1); | |
} | |
scope.$on('focusOn', function(e, name) { | |
if (name === attr.focusOn) { | |
elem[0].focus(); | |
} | |
}); | |
}; | |
}) | |
.factory('focus', function($rootScope, $timeout) { | |
return function(name) { | |
$timeout(function() { | |
$rootScope.$broadcast('focusOn', name); | |
}); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment