Skip to content

Instantly share code, notes, and snippets.

@brutuscat
Created December 16, 2013 17:38
Show Gist options
  • Save brutuscat/7991006 to your computer and use it in GitHub Desktop.
Save brutuscat/7991006 to your computer and use it in GitHub Desktop.
focus-on directive that will listen $on the event emitted and will focus the element when event is triggered

Example usage:

In a template:

<element focus-on="modelUpdated">
</element>

In a controller:

$scope.$broadcast('modelUpdated');
app.directive('focusOn', function() {
return {
restrict: 'A',
scope: {
'continuePropagation': '@'
},
link: function(scope, element, attrs) {
scope.$on(attrs.focusOn, function(event) {
element[0].focus();
if (!scope.continuePropagation && event.stopPropagation) {
event.stopPropagation();
}
});
},
};
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment