In a single-page application (SPA), navigation between different views happens without reloading the entire page. AngularJS facilitates this through routing, allowing users to navigate between views seamlessly. This tutorial covers the basics of routing in AngularJS using the ngRoute
module, enabling you to create routes and configure views for your SPA.
Table of Contents
- What is Routing in AngularJS?
- Setting Up ngRoute
- Configuring Routes
- Creating Views for Each Route
- Navigation Between Routes
- Conclusion
What is Routing in AngularJS?
Routing is the mechanism in SPAs that enables users to switch between different views without reloading the entire page. In AngularJS, routing is accomplished with the ngRoute
module, which allows you to define multiple routes and map them to specific views and controllers.
Using routing in AngularJS improves user experience by enabling a smoother and faster navigation process, as only the necessary content is loaded dynamically instead of reloading the entire page.
Setting Up ngRoute
The ngRoute
module is an external AngularJS module, so you need to include it separately. You can add the angular-route.min.js
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>
After adding the script, inject the ngRoute
module into your AngularJS application:
angular.module('myApp', ['ngRoute']);
Configuring Routes
To configure routes, use the $routeProvider
service in AngularJS. The config
function allows you to set up different routes, associating each route with a specific template and controller.
angular.module('myApp')
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'HomeController'
})
.when('/about', {
templateUrl: 'about.html',
controller: 'AboutController'
})
.when('/contact', {
templateUrl: 'contact.html',
controller: 'ContactController'
})
.otherwise({
redirectTo: '/'
});
});
In this example, we have three routes:
- “/” – Loads
home.html
and uses theHomeController
. - “/about” – Loads
about.html
and uses theAboutController
. - “/contact” – Loads
contact.html
and uses theContactController
. - Otherwise – Redirects to
"/"
if the URL does not match any defined routes.
Creating Views for Each Route
Each route should have a corresponding HTML template (view) and controller. For instance, create home.html
, about.html
, and contact.html
as separate HTML files for each route.
home.html
<h2>Welcome to the Home Page</h2>
<p>This is the main landing page.</p>
about.html
<h2>About Us</h2>
<p>Learn more about our company and mission.</p>
contact.html
<h2>Contact Us</h2>
<p>Get in touch with us through this page.</p>
Navigation Between Routes
In AngularJS, you can use ngRoute
in combination with ng-view
to dynamically load the correct view for each route. Place an ng-view
directive in your main HTML file where you want the route content to display.
<html ng-app="myApp">
<head>
<title>AngularJS Routing Example</title>
</head>
<body>
<h1>Single Page Application</h1>
<nav>
<a href="#!/">Home</a>
<a href="#!/about">About</a>
<a href="#!/contact">Contact</a>
</nav>
<div ng-view></div>
<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>
</body>
</html>
The ng-view
directive serves as a placeholder that loads the content of each view based on the current route. In this example, clicking on each link navigates to the respective view without reloading the page.
Conclusion
Routing and navigation are essential for creating a dynamic single-page application in AngularJS. By setting up ngRoute
and configuring routes with $routeProvider
, you can define multiple views and navigate between them seamlessly. Mastering routing in AngularJS enables you to build efficient, user-friendly SPAs.