AngularJS routing is a core feature that allows developers to create single-page applications (SPAs). Instead of reloading an entire webpage, routing dynamically updates the view based on the current URL, making your application faster and more user-friendly.
In this tutorial, you will learn how AngularJS routing works, how to set it up, and how to build dynamic and navigable SPAs. For more in-depth AngularJS guides, visit The Coding College.
Why Use AngularJS Routing?
- Faster Navigation: No full page reloads, resulting in a smoother user experience.
- Modular Design: Keeps your application organized by separating views into templates.
- Custom URL Management: Enables deep linking, allowing users to bookmark or share specific views.
- Enhanced User Experience: Seamlessly navigate between views without losing context.
Setting Up AngularJS Routing
To use routing, you’ll need the ngRoute
module.
Step 1: Include Required Scripts
Add AngularJS and the ngRoute
script to your HTML file.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-route.min.js"></script>
Step 2: Declare the ngRoute
Dependency
Include the ngRoute
module in your AngularJS app.
var app = angular.module('myApp', ['ngRoute']);
Step 3: Configure Routes
Use the $routeProvider
service to define application routes.
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'homeCtrl'
})
.when('/about', {
templateUrl: 'about.html',
controller: 'aboutCtrl'
})
.when('/contact', {
templateUrl: 'contact.html',
controller: 'contactCtrl'
})
.otherwise({
redirectTo: '/'
});
});
Basic AngularJS Routing Example
Folder Structure
project/
│
├── index.html
├── home.html
├── about.html
├── contact.html
├── app.js
index.html
<!DOCTYPE html>
<html>
<head>
<title>AngularJS Routing</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-route.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<nav>
<a href="#/">Home</a> |
<a href="#/about">About</a> |
<a href="#/contact">Contact</a>
</nav>
<div ng-view></div>
</body>
</html>
app.js
var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'homeCtrl'
})
.when('/about', {
templateUrl: 'about.html',
controller: 'aboutCtrl'
})
.when('/contact', {
templateUrl: 'contact.html',
controller: 'contactCtrl'
})
.otherwise({
redirectTo: '/'
});
});
app.controller('homeCtrl', function ($scope) {
$scope.message = 'Welcome to the Home Page!';
});
app.controller('aboutCtrl', function ($scope) {
$scope.message = 'Learn more About Us.';
});
app.controller('contactCtrl', function ($scope) {
$scope.message = 'Contact us at [email protected].';
});
home.html
<h1>Home</h1>
<p>{{ message }}</p>
about.html
<h1>About</h1>
<p>{{ message }}</p>
contact.html
<h1>Contact</h1>
<p>{{ message }}</p>
Advanced Features
1. Route Parameters
You can pass parameters to routes for more dynamic behavior.
$routeProvider.when('/product/:id', {
templateUrl: 'product.html',
controller: 'productCtrl'
});
Controller Example
app.controller('productCtrl', function ($scope, $routeParams) {
$scope.productId = $routeParams.id;
});
HTML Template
<h1>Product Details</h1>
<p>Product ID: {{ productId }}</p>
2. Resolving Data Before Navigation
The resolve
property ensures that necessary data is loaded before the route is activated.
$routeProvider.when('/dashboard', {
templateUrl: 'dashboard.html',
controller: 'dashboardCtrl',
resolve: {
userData: function (userService) {
return userService.getUserData();
}
}
});
Best Practices
- Organize Routes
Group related routes in separate files or modules for better scalability. - Fallback Routes
Use.otherwise()
to define a fallback route for undefined URLs. - Lazy Loading
Load only necessary templates and controllers to optimize performance. - Secure Routes
Restrict access to specific routes by checking user authentication.
Conclusion
AngularJS routing empowers developers to build dynamic and interactive SPAs with minimal effort. By understanding and implementing routing effectively, you can create applications that are faster, more user-friendly, and scalable.