Skip to content

Instantly share code, notes, and snippets.

@gengue
Last active September 12, 2017 20:28
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 gengue/9d5d7394c85a6c5b915c4f249ac2508a to your computer and use it in GitHub Desktop.
Save gengue/9d5d7394c85a6c5b915c4f249ac2508a to your computer and use it in GitHub Desktop.
Focus directive angularjs
angular.module('app.layouts').directive('focusMe',
function($timeout, $parse) {
return {
link: function(scope, element, attrs) {
var model = $parse(attrs.focusMe);
scope.$watch(model, function(value) {
if(value === true) {
$timeout(function() {
element[0].focus();
});
}
});
element.bind('blur', function() {
console.log('blur')
scope.$apply(model.assign(scope, false));
})
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment