Routing in Angular JS using Angular UI Router
Last Updated :
19 Feb, 2019
AngularJS is a front-end web application framework based on JavaScript and is maintained by Google.
AngularJS interprets the attributes of HTML as directives to bind input/output components to a model represented by standard JavaScript variables.
Pre-requisites:
- HTML
- CSS
- JavaScript
- AngularJS
Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.
Angular-UI-Router has stateProvider method which is used to create routes/states in application. State Provider takes state name and state configurations as parameters.
Syntax:
$stateProvider
.state('StateName', {
url: 'Url pattern for this State',
template: "Html content",
controller: "Name of the Controller for this state"
});
Instead of template, templateUrl can be used and given the path of the HTML file to render for the state.
Example:
$stateProvider
.state('Home', {
url: '/home',
templateUrl: "home.html",
controller: "HomeCtrl"
});
Simple project to navigate between the routes to demonstrate the use of the Angular-UI-Router module.
Pre-requisites: Node.js and
npm
To run and install http-server node module to host demo app.
Steps to perform the operation:
1. Create a directory structure as below:
routingDemo
--app.js
--index.html
--nav.html
2. Create
nav.html file as below. This file consist of nav bar title and contents of it.
3. Create
index.html file as below. All the required dependencies are included in this file along with
nav.html file and definition of
ui-view where content of different routes will be rendered.
Explanation:
- All the dependencies are included via CDN in the head tag.
- nav.html file is included to the index.html page in the body tag
- The last division in body defines ui-view div where the content of different routes will be rendered.
Note:If it does not work, replace the second and third script with below:
<script src="angular.min.js"></script>
<script src = "
https://unpkg.com/@uirouter/[email protected]/release/angular-ui-router.min.js">
</script>
4. Create
app.js file as below. This is the application file with routes information and actions to be performed through controller.
javascript
// declares application module with name "myApp"
// inject ui.router dependency
var app = angular.module('myApp', [ "ui.router" ]);
// define route configurations inside app.config
// injecting dependencies
app.config(function($stateProvider, $locationProvider,
$urlRouterProvider) {
// creating routes or states
$stateProvider
.state('Home', {
url : '/home',
template : "<h1>Home Page</h1>",
controller : "HomeCtrl"
})
.state('Login', {
url : '/login',
template : "<h1>Login Page</h1>",
controller : "LoginCtrl"
})
.state('Signup', {
url : '/signup',
template : "<h1>Signup Page</h1>",
controller : "SignupCtrl"
});
// Redirect to home page if url does not
// matches any of the three mentioned above
$urlRouterProvider.otherwise("/home");
});
// create empty controllers for the states as we are
// not doing anything but just displaying message
app.controller('MainCtrl', function() {});
app.controller('HomeCtrl', function() {});
app.controller('LoginCtrl', function() {});
app.controller('SignupCtrl', function() {});
5. To run the above demo app in the browser, install
http-server node module. To install the
http-server module use the following command:
npm install http-server -g
6. After Installing:
--From
routingDemo folder, run following command:
http-server
Above command will host the demo app on port 8080. app can be accessed using below link:
localhost:8080/
7. If this application is accessed via browser, Output will be as below:
8. After clicking on
Login tab in the
nav bar, Output will be as below:
9. Clicking on
Sign up tab in the
nav bar, Output will be as below:

Three routes namely
Home,
Login and
Sign up are created in this sample application.
Applications:
Routes are important for Single Page applications as they provide different functionalities to the application on the same page.
Routes creation and manipulation is easy with the angular-ui-router module.
References:
Similar Reads
How to use Angular MatTabsModule with routing in Angular 17?
The Angular Material library provides a tab component called MatTabsModule that allows you to organize content into separate tabs. When combined with Angular routing, you can create a seamless navigation experience where each tab loads its content from a different route. In this article, we'll explo
4 min read
What is Routing and Nested Routing in Angular 9/8 ?
In this article, we will learn the routing & nested routing concept in Angular. We will implement the concept to establish routing between different components by making their routes when a user clicks the link, it will be navigated to a page link corresponding to the required component. Let's u
5 min read
Angular Authentication Using Route Guards
In Angular, implementing authentication is important for securing routes and controlling access to different parts of the application based on user authentication status. Angular provides a powerful feature called Route Guards, and among them, the Auth Guard is used to control navigation based on th
12 min read
How to use Angular Material in Angular 17?
Angular Material is a comprehensive UI component library designed specifically for Angular applications. With its large collection of pre-built components and seamless integration with Angular, Angular Material simplifies the process of creating visually appealing and responsive user interfaces. In
2 min read
Angular CLI | Angular Project Setup
Angular is an open-source front-end web application framework that is used for building single-page and complex web applications. By default, angular uses TypeScript for creating logic but as the browser doesn't know typescript it converts typescript into javascript in order to make typescript under
3 min read
How to Change Image Source URL using AngularJS ?
Here the task is to change the source URL of the image with the help of AngularJS.Approach: The approach is to change the URL of the image when the user click on button. When a user clicks on a button then a method is called along with the new URL, that method replaces the new URL with the old one i
2 min read
Angular 7 | Angular Data Services using Observable
Observables Observable manage async data and a few other useful patterns. Observables are similar to Promises but with a few key differences. Unlike Promises, Observables emit multiple values over time. In real scenarios, web socket or real-time based data or event handlers can emit multiple values
4 min read
Angular PrimeNG Paginator Styling
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn about Angular PrimeNG Paginator Styling. The Paginator Component is u
3 min read
Styling Active Router Link in Angular
Styling the active router link in angular allows the user to differentiate between the active router link and inactive router links. Angular provides a special mechanism to work with active router links. Approach: Create the Angular app to be used.Create the header component that contains the navi
2 min read
How to make alert using Angular UI Bootstrap ?
In this article, we will see how to make alerts using Angular UI bootstrap. Angular UI Bootstrap is an Angular JS framework created by Angular UI developers for providing better UI which can be used easily. Alerts directive is used for generating alerts from static and dynamic model data. Syntax:
2 min read