Created
November 24, 2015 01:19
-
-
Save ProLoser/49b58e55b21457e9bf08 to your computer and use it in GitHub Desktop.
Focuses on input/element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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