Friday, November 21, 2014

Angular.js ui-router: Focus on view

In an Angular.js application we are building, we use the great ui-router module as a replacement for the default ngRoute module.

From the documentation:

AngularUI Router is a routing framework for AngularJS, which allows you to organize the parts of your interface into a state machine. Unlike the $route service in the Angular ngRoute module, which is organized around URL routes, UI-Router is organized around states, which may optionally have routes, as well as other behavior, attached.

States are bound to named, nested and parallel views, allowing you to powerfully manage your application's interface.

It offers a lot more flexibility than ngRoute and is our default choice when building Angular.js applications.

This doesn’t mean that everything is perfect. Last week we got a bug from a user mentioning that the page scrolled down when loading the Angular.js application. We discovered that this is caused by the ui-route module which has autoscroll enabled by default.

Luckily it’s easy to change this, just add the following attribute to your ui-view location:

<div ui-view autoscroll="false"></div> 

More information about this attribute can be found here: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#autoscroll

That’s it!

No comments: