Home » JavaScript » Angular.js » AngularJS UI-Router – Introduction

About Antonio Morales

Antonio Morales
Antonio has graduated from Computer Engineering in the University of Seville. He also holds a Master degree in Engineering and Software Technology from the University of Seville as well. During his working life he has been involved in a large number of projects both national and European projects from Digital Identity and Security to Semantic Search, NLP and Machine Learning. He is also committer in the Apache Software Foundation in the Apache Stanbol project and collaborator of other Apache projects. Mainly involved in projects based on Java and Semantic technologies, he loves all the new technologies both front-end and back-end side, always eager to learn new technologies to be applied to new projects.

AngularJS UI-Router – Introduction

This is the first post of a series of 3, presenting AngularJS UI-Router library:

 
 
 
 

Introduction

AngularJS is a well-known Javascript framework nowadays. It provides a great support to create SPA (Single Page Applications), easing some of the tedious stuff needed when writing client-side Javascript applications (boilerplate code, DOM bindings, URL routing, etc).

AngularJS is shipped with all the necessary things to build expressive applications without depending on third-party libraries. Nevertheless, in very short time, AngularJS has created a great and very active community around it, creating different AngularJS components over well-known Javascript libraries, like Bootstrap, jQuery UI, etc. These new components are very useful to improve and maximize the potential of our applications.

One of the key points when creating single page applications is the routing mechanism used. The main objective of routing mechanism is to achieve that the user navigates the application like a normal site, but also avoiding or reducing the annoying waiting time due to a page change (page transition).

AngularJS already provides support for routing through the ngRoute module. ngRoute is a basic routing library where we can specify just one view and controller for any route (URL).

But there are situations when we need more control over routes or we need composite or nested views and we can’t achieve it easily with ngRoute.

The AngularJS community has created the AngularUI library. It provides different components which can be used in our AngularJS applications. This library has been broken down into several modules, so that, we can choose only the components we are interested in, instead of including the whole library.

AngularJS UI-Router

The UI-Router is a routing framework for AngularJS built by the AngularUI team, providing a different approach than ngRoute. Unlike ngRoute, it allows us to change the application views based on the state of the application and not just the URL route. In other words, it converts the parts of the application interface into a state machine.

As stated before, UI-Router is organised around states, which may optionally have routes, as well as other kind of behaviours attached. States are bound to named, nested and parallel views, allowing powerfully managing the application’s interface. With this approach, views and routes aren’t tied to the site URL, so we can change the parts of the site using the routing even if the URL does not change.

The library provides a lot of extra control in views: nested views, multiple views on the same page, etc. In order to have a finer grain control over the application routing, UI-Router is a great library to take advantage of.

States VS URL Routes

One of the drawbacks of routes is that we have to tie a route with an URL, having to use ngInclude or other method to change different parts of the page managed by this route. Using states, we are not tied to the URLs to modify parts of the application. States allows us to change parts of the pages using its routing process even when we don’t need to change the URL. Like ngRoute, uiRoute allows to configure the states, routing, controllers and views using the .config() command of AngularJS but there are some reasons because uiRoute has some advantages over the features provided by ngRoute:

  • UI-Router allows nested views and multiple views in each state. States are bound to named, nested and parallel views. It allows to manage the application’s interface in a more powerful way
  • With states you can access different information about the states and pass information between them
  • You can determine in which state you are on each moment, so you can act over the UI accordingly, for example, putting an active class to the navigation element for that state
  • States can be bound to URLs or not but states bound to URLs will be updated every time the URL changed using the custom uiRouter element ui-sref. Very handy for URLs based applications
  • State decorators can be used to modify dynamically the states configuration. It can be used to add custom functionality to ui-router, for example inferring template URLs based on the state name instead of configure them on each state beforehand

Conclusions

AngularJS routing through ngRoute is a great feature to create URL-based client side applications. But there are situations where we need more control over the components used on each URL.

For these use cases, AngularJS UI-Router library comes to the rescue because it allows us to break down our application into states where the states can have children states or multiple components. UI-Route library gives a lot of extra control in our views providing a finer grain control when creating more complex applications.

In a glance, UI-Router can be seen like ngRoute with more features but under the hood it is quite different. All the additional features presented above are very useful in the development of larger applications.

References

AngularJS UI-Router source code and wiki
AngularJS UI-Router documentation

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments