An attribute to focus an element when it appears and by a value.
2 people use it
Author: 585755?v=3 deltreey


An attribute to focus an element when it appears and by a value.

It's a simple angularjs directive that focuses an input when it appears and whenever you set a value to true. This means that this works to set the focus on page load, and as an element is created as part of an ng-repeat and of course it can be set manually.

Because it's such a small amount of code, it should be able to read and understand so you can even tweak it for your needs. In fact, I encourage it!

Available on Bower

bower install angular-simple-focus

How To

Simply include the simple-focus module

Then use the simple-focus attribute on your html elements

Note that the simple-focus attribute must have a value, but that value is arbitrary, so pick whatever you want. The value is pulled in from your controller's $scope, so $scope.someValue in my controller is the value for the simple-focus attribute above. If I set $scope.someValue = true; in my controller, the input would get focus.

There are lots of different directives just like this one. And lots of people have attempted to explain it in examples In fact, this code is almost an exact copy of this example gist. I built this one because none of the other ones worked exactly this way, most of them were just examples, not a project that people could feel like they were a part of, and most of them were much more complicated both to use and to develop for. This one is simple. If it doesn't perfectly fit your needs, I have confidence it will help you understand enough to redesign it for yourself. I'll even happily accept pull requests and issues to improve it.

Check out the github pages example here:

comments powered by Disqus
This page was last updated over 1 year ago.