Created
August 31, 2014 19:04
-
-
Save bfillmer/b66327027b51836f422a to your computer and use it in GitHub Desktop.
Angular JS Focus Directive
This file contains 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
(function(){ | |
/** | |
* Emulates ng-show for focus, i.e.: | |
* | |
* <input ng-focus="truthyValue"> | |
* | |
* Setting truthyValue to true focuses said input. | |
*/ | |
app.directive('ngFocus', function($timeout){ | |
return { | |
restrict: 'A', | |
link: function($scope, $element, $attr){ | |
$scope.$watch($attr.ngFocus, function(value){ | |
if(value){ | |
$timeout(function(){ | |
if($scope.$eval($attr.ngFocus)){ | |
$element[0].focus(); | |
} | |
}, 0, false); | |
} | |
}); | |
} | |
}; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment