Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created November 15, 2012 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save revolunet/4075928 to your computer and use it in GitHub Desktop.
Save revolunet/4075928 to your computer and use it in GitHub Desktop.
Angular focusMe directive, watch example
// copied from http://plnkr.co/edit/1LhUJ4?p=preview
angular.module('myApp', [])
.directive('focusMe', function () {
return {
restrict: 'A',
scope: {
focusMe: '='
},
link: function (scope, elt) {
scope.$watch('focusMe', function (val) {
if (val) {
elt[0].focus();
}
});
}
};
});
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
</head>
<body ng-app="myApp">
<input type="checkbox" ng-model="checked">
<input focus-me="checked">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment