Skip to content

Instantly share code, notes, and snippets.

@alanmoo
Last active December 20, 2015 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alanmoo/6089544 to your computer and use it in GitHub Desktop.
Save alanmoo/6089544 to your computer and use it in GitHub Desktop.
When repeating radio inputs, use this AngularJS directive to make the browser focus on the active input.
angular.module('myApp', [])
.directive('focusActive', function(){
return {
restrict: "A",
link: function(scope, element, attrs) {
attrs.$observe('checked', function(){
if (element[0].checked){
element.focus();
}
})
}
}
});
<!-- Use by calling focus-active as input attribute-->
<div ng-repeat="status in statuses">
{{status}}<input focus-active type="radio" ng-model="person.location" value="{{status}}">
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment