<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rommel Santor&#039;s Clog &#187; autofocus</title>
	<atom:link href="https://rommelsantor.com/clog/tag/autofocus/feed/" rel="self" type="application/rss+xml" />
	<link>https://rommelsantor.com/clog</link>
	<description>my exploits and misadventures in programming</description>
	<lastBuildDate>Thu, 04 Feb 2016 12:56:01 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>AngularJS Auto-Focus Input After Router Template is Loaded in SPA</title>
		<link>https://rommelsantor.com/clog/2015/10/14/angularjs-auto-focus-input-after-router-template-is-loaded-in-spa/</link>
		<comments>https://rommelsantor.com/clog/2015/10/14/angularjs-auto-focus-input-after-router-template-is-loaded-in-spa/#comments</comments>
		<pubDate>Wed, 14 Oct 2015 10:46:16 +0000</pubDate>
		<dc:creator><![CDATA[rommel]]></dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[autofocus]]></category>
		<category><![CDATA[ngRoute]]></category>
		<category><![CDATA[SIngle Page App]]></category>
		<category><![CDATA[SPA]]></category>

		<guid isPermaLink="false">http://rommelsantor.com/clog/?p=422</guid>
		<description><![CDATA[It&#8217;s been just a little while (oh, only about 3.5 years) since I last published any new posts here. Too busy. At the moment I&#8217;ve been delving head-first into some modern frameworks like AngularJS and Bootstrap, so I plan to document things I learn along the way that I think will help other developers also new [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s been just a little while (oh, only about 3.5 years) since I last published any new posts here. Too busy.</p>
<p>At the moment I&#8217;ve been delving head-first into some modern frameworks like AngularJS and Bootstrap, so I plan to document things I learn along the way that I think will help other developers also new to these technologies who might come up against some of the same issues or who might just find interesting the things I&#8217;m discovering.</p>
<p>Without further ado, this is my first of such posts, which is a little more advanced than just starting to learn everything from scratch, but I do plan to back-track and cover more in future posts the basics of using AngularJS.</p>
<p><span id="more-422"></span>
<div style="float: left; margin: 0 10px 10px 0;"><script type="text/javascript">// <![CDATA[
google_ad_client = "ca-pub-7639656561235411";
/* Square (250x250) */
google_ad_slot = "8522038794";
google_ad_width = 250;
google_ad_height = 250;
// ]]&gt;</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">// <![CDATA[

// ]]&gt;</script></div>
</p>
<p>If you&#8217;re developing a Single Page Application (SPA) with Angular, you may find that loading an HTML fragment with a form input that should be auto-focused might not work. Assuming you&#8217;ve already set up ngRoute to dynamically control the routing of your application, the simplest way I came up with to set focus on an input after ensuring the HTML template is fully loaded is to define a callback for <code>$routeChangeSuccess</code> which is invoked after the route has finished changing. The best place to respond to that event is in a run block, since it&#8217;s an application-wide action we&#8217;re taking. The trick is that the DOM may still not be ready for you to set focus at that point, but setting focus inside a $timeout will ensure the $digest loop has ended.</p>
<p>The other, more obvious decision was to ensure the input I want to auto-focus has the &#8220;autofocus&#8221; attribute so it can be targeted using a simple jQuery selector. Below is a bit of sample code demonstrating what I just described with comments, so hopefully it&#8217;s all pretty obvious to you (and it <i>should</i> be if you&#8217;re using Angular yourself).</p>
<p>[<a href="https://github.com/rommelsantor/ng-dynautofocus" target="_blank">GitHUB &raquo;</a>]</p>

<pre class="js:nocontrols:nogutter">&lt;script&gt;
// first thing's first; initialize the app
var app = angular.module('yourAppName', ['ngRoute']);// '

//... do other things like set up your routing (not covered here)

// set up a Run Block
app.run(function _forceAutofocus($rootScope, $timeout) {

  // tell Angular to call this function when a route change completes
  $rootScope.$on('$routeChangeSuccess', function() {
    // we can't set focus at this point; the DOM isn't ready for us

    // instead, we define a callback to be called after the $digest loop
    $timeout(function(){
      // once this is executed, our input should be focusable, so find (with jQuery)
      // whatever is on the page with the autofocus attribute and focus it; fin.
      $('[autofocus]').focus();
    });
  });

});
&lt;/script&gt;</pre>

<p><script type="text/javascript">// <![CDATA[
//< ![CDATA[
$("pre").attr({name:'code'});
// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>https://rommelsantor.com/clog/2015/10/14/angularjs-auto-focus-input-after-router-template-is-loaded-in-spa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
