Routing In AngularJS Using UI-Router

Routing In AngularJS Using UI-Router

In this tutorial, we will see how to do Routing In AngularJS Using UI-Router. It is a very simple to create a route in AngularJS using UI-Router, all you need to know is some basic concept of AngularJS.

Check out the demo on how to do Routing In AngularJS Using UI-Router.

What Is AngularUI Router?

Angular UI-Router is a client-side Single Page Application routing framework for AngularJS.
UI-Router applications are modeled as a hierarchical tree of states. UI-Router provides a state machine to manage the transitions between those application states in a transaction-like manner.



So let’s start with building the app structure. We will be specifying every component in a different folder so starting with folder structure.


|--- index.html
|     |---shared(folder)
|     |        |---header(folder)
|     |        |        |---header.html
|     |        |
|     |        |---footer(folder)
|     |                 |---footer.html
|     |
|     |---components(folder)
|     |            |---home(folder)
|     |            |      |---header.html
|     |            |
|     |            |---contact(folder)
|     |            |      |---contact.html
|     |            |
|     |            |---about(folder)
|     |                   |---about.html
|     |
|     |---app.routes.js  


To create a routing process we will be using the AngularJS,
Angular-UI-Router and the Bootstrap(optional) for design purpose.
let’s start with implementing the dependencies.
Now include the required libraries into our index.html


<html lang="en">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  <body ng-app="demo">
      <div ui-view="header"></div>
      <div ui-view=""></div>
      <div ui-view="footer"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" ></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js" ></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js" ></script>
      <!-- Main app routes js -->
      <script src="app/app.routes.js"></script>

Here you can see we have included Angular.min.js, Angular-UI-router and our main app.routes.js at bottom of the page, and the bootstrap.min.css at the top of the page.
and also we have included ui-view for different views such as header, footer and the middle content to be loaded in each state.

And if you are using the package manager(npm) you can download through a command in terminal:


$ npm install angular --save // AngularJS

$ npm install angular-ui-router --save // angular UI-router

$ npm install bootstrap@3 --save // Latest Bootsrap v 3.x.x


After setting up the index file, we will start building our components in which each component will be separated by folder wise.
let’s first implement the header view from there we will be linking our components using UI-router, as you can see our directory structure we have separated header and footer in the shared folder and other pages in components.


<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      <a class="navbar-brand" href="#">Logo</a>
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a ui-sref="home">Home</a></li>
		<li><a ui-sref="about">About</a></li>
		<li><a ui-sref="contact">contact</a></li>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->

As you can see instead of href we will be using ui-sref , which will route the components which we will be assigned in the app.routes.js file.
Similarly create your own custom components i.e header, footer, home page, etc as the directory structure shown.
See the folder structure with demo over HERE.

App Router

Now we have to create the demo that we already applied to our body in the index.html file.
start building the app through the app.routes.js, first of all, we will create an angular module for the component that we will be using.


// demo : app name
// ui.router : Angular-ui-router module
// app.routes : custom routes module
var app = angular.module('demo', ['ui.router', 'app.routes']);

you can see we have created our demo angularApp by implementing the module i.e ui-router imported from the CDN link and the other one app.routes, in that module we will create our custom routes. So let’s begin with building module.


var app = angular.module('demo', ['ui.router', 'app.routes']);
.config(function($stateProvider, $urlRouterProvider) {

// create default view 

// home states and nested view
.state('home', {
	url: "/home",
	views : {
		"" : {
		"header" : {
		"footer" : {
// about states and nested view
.state('about', {
	url: "/about",
	views : {
		"" : {
		"header" : {
		"footer" : {
.state('contact', {
	url: "/contact",
	views : {
// ... paths for contact view as define above

Here we have a .state() for home, about and for contact. In the home, we are using the template file home.html. so as for other pages.

NOTE: The app should be run on localhost server to see the working of the ui-router

Get Some More Angular Here!

About This Author

Hi This Is Rehmaanali. Blogging Is Just A Hobby. Graduate In Bsc Computer Science

Post A Reply