Introduction
In the present scenario, AngularJS is the most trusted framework for the creation of Single Page Applications and for SPA (Single Page Application), and 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, shopping cart or blog entry.
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 [email protected]’.
- Download directly from cdnjs.
- Add the script file references in HTML page in the order given below.
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js" type="text/javascript"></script>
- <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js" type="text/javascript"></script>
- <script src="helloworld.js"></script>
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.
Add Angular module in helloworld.js
- var myApp = angular.module('helloworld', ['ui.router']);
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.

Join the conversation! Your thoughts help the community grow.