UI-Routing – Basics

Introduction

In the present scenario, AngularJS is the most trusted framework for the creation of Single Page Applications (SPA), and UI-Routing is one of the most important aspects. We want to keep our navigation feeling like a normal site and want to load the pages without refreshing the Browser. For this, we have to go through Angular routing i.e. ngRoute method.

Also, find my article to remove hash from URL here.

Overview

Before starting, you must have a basic knowledge about Angular UI.

What is Angular ui-Router?

ui-Router is a routing framework built by Angular UI team. It is different from ngRoute as it changes your views, which are based on the State of the Application and not by route URL.

It provides features such as States, Views, URLs, Parameter, Resolve Data and Transition.

States vs URL Route

Ui-router Applications behaves like a state machine. Let us take an example of an Application in which each feature is a set of the state for the Application. Only one State can be active at one time and the user must navigate from one State to another to activate a typical feature.

Some examples of States might be dashboard, messages, shoppingcart or blogentry.

State will be further explained in a future article.

While using ngRoute, one has to write ngInclude or some other methods and this can be confusing to a user. This method is appropriate in top-down view of the Application as all the States, routing and views are handled in on .config().

Sample Application

  • Get UI-Router for AngularJS code, using your preferred mechanism.
    • Using npm: ‘npm install –save angular-ui-router@next’.
    • Using bower- ‘bower install angular-ui-router@1.0.0-beta.3’.
    • Download directly from cdnjs.
  • Add the script file references in HTML page in the order given below.

Note

Here, I have used online CDN reference for AngularJS and UI-routerJS. Also, the order of the file must be, as shown above. UiRouter file must be placed after AngularJS file, followed by placing other JS files.

Create Angular module

Add Angular module in helloworld.js

Here, we have added dependency for ui-router in our main module ‘helloworld’.

Creating State

State is a basic building-block for UI-Router Application. This script object is a simple object definition of State definition.

This State definition has 3 properties, which are given below.

  • name
    The name of State. In this case, it is named hello
  • URL
    When the State is active, the Browser’s URL will be /hello.
  • template
    The template, which is to be rendered when State is active will be loaded in the ViewPort.

Register States

Create another State ‘about’. Register both the $stateProvider in .config() block.

Because $stateProvider is an Angular Provider, you must inject it into a .config() block using the code.

ViewPort

Add <ui-view> tag i.e. ViewPort in your HTML file. The <ui-view> tag is a ui-router ViewPort. Whenever the State is activated, the State’s view i.e. template will be loaded in the ViewPort.

Links

Add some <ui-href> tag to show the links. When clicked, the links will be active in a State.

A ui-sref is a Directive, which behaves similar to HTML href. Instead of referencing a URL like HREF, it references a State and ui-sref Directive automatically builds a href attribute (<a href=…></a>), which is based on your State’s URL.

Active link

Add <ui-sref-active=”active”> tag to the ui-sref links. This Directive will add the active CSS class to the link when the target State is active.

Finally, add the style tag and the active class to style the active link as Red and bold.

Conclusion

Hence, we have created a simple demo for routing with UI-Router. In the next part, we will cover UI-Router States in detail.

Arif Khoja
Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.
Arif Khoja on FacebookArif Khoja on GoogleArif Khoja on InstagramArif Khoja on LinkedinArif Khoja on Twitter

Leave a Reply

Top
%d bloggers like this: