Skip to content

Instantly share code, notes, and snippets.

@ProLoser
Created November 24, 2015 01:19
Show Gist options
  • Save ProLoser/49b58e55b21457e9bf08 to your computer and use it in GitHub Desktop.
Save ProLoser/49b58e55b21457e9bf08 to your computer and use it in GitHub Desktop.
Focuses on input/element
/**
* focusOn - Focuses an input on scope event
*
* @example
* <input focus-on="someEventName">
* or
* <input focus-on="focus-row-{{$index}}">
* or
* <p focus-on="anotherEvent"></p>
* ...
* $scope.$broadcast('someEventName');
* $scope.$broadcast('focus-row-2');
* $scope.$broadcast('anotherEvent');
*
*/
angular.module('ts.utils').directive('focusOn', function(){
return {
link: function($scope, $element, $attrs) {
var listener = angular.noop;
$attrs.$observe('focusOn', function(newVal){
// Stop listening to old event name
listener();
// Listen to new event name
listener = $scope.$on(newVal, function(){
if ($element.is('input:not([type="button"]),textarea'))
$element[0].focus();
else
$element[0].scrollIntoView();
});
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment