Skip to content

Instantly share code, notes, and snippets.

@matthewbednarski
Created October 1, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewbednarski/d3a580c78b873779b931 to your computer and use it in GitHub Desktop.
Save matthewbednarski/d3a580c78b873779b931 to your computer and use it in GitHub Desktop.
An angular directive/service for giving focus to elements with a tabIndex
(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