Skip to content

Instantly share code, notes, and snippets.

@benjamincharity
Last active October 7, 2015 17:31
Show Gist options
  • Save benjamincharity/2da28796b82f158f9540 to your computer and use it in GitHub Desktop.
Save benjamincharity/2da28796b82f158f9540 to your computer and use it in GitHub Desktop.
Focus an input.
angular.module('common')
.directive('focusOn', function() {
'use strict';
return function(scope, elem, attr) {
scope.$on('focusOn', function(e, name) {
if (name === attr.focusOn) {
elem[0].focus();
}
});
};
});
angular.module('common')
.factory('Focus', function(
$rootScope, $timeout
) {
'use strict';
return function(name) {
console.log('in focus factory: ', name);
$timeout(function (){
$rootScope.$broadcast('focusOn', name);
});
};
});
<input focus-on="yearExp">
angular.module('common')
.controller('FooCtrl', function(Focus) {
Focus('yearExp');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment