π£οΈ AngularJS Routing β Navigate Between Views Using ngRoute (2025 Guide)
π§² Introduction β Why Learn AngularJS Routing?
Single Page Applications (SPAs) rely heavily on client-side routing to navigate between views without reloading the page. AngularJS supports this functionality through its ngRoute
module, which allows you to map URLs to templates and controllersβcreating smooth, dynamic navigation within your app.
Routing is essential for:
- Dividing your application into manageable views
- Navigating between pages like Home, About, Contact
- Loading content dynamically based on route
π― In this guide, youβll learn:
- How AngularJS routing works using
ngRoute
- How to set up route configurations
- Create and link views dynamically
- Use route parameters for dynamic content
- Handle undefined routes (404 fallback)
βοΈ Prerequisites
- AngularJS library
angular-route.js
included via CDN or local file
CDN example:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-route.min.js"></script>
π Basic Folder Structure
/angularjs-routing-app
β
βββ index.html
βββ app.js
βββ views/
β βββ home.html
β βββ about.html
β βββ contact.html
β
Step 1: Configure Routes with $routeProvider
var app = angular.module('myApp', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller: 'HomeCtrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl'
})
.when('/contact', {
templateUrl: 'views/contact.html',
controller: 'ContactCtrl'
})
.otherwise({
redirectTo: '/'
});
});
β Step 2: Define Controllers
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 support@example.com";
});
β
Step 3: Main Template (index.html
)
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>AngularJS Routing</title>
<script src="angular.min.js"></script>
<script src="angular-route.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<nav>
<a href="#!/">Home</a> |
<a href="#!/about">About</a> |
<a href="#!/contact">Contact</a>
</nav>
<div ng-view></div>
</body>
</html>
π Sample View β home.html
<h2>Home</h2>
<p>{{ message }}</p>
π§Ύ The ng-view
directive loads the content based on the current URL hash.
π Using Route Parameters
β Define a Route with Parameter
.when('/user/:id', {
templateUrl: 'views/user.html',
controller: 'UserCtrl'
})
β Access the Parameter in Controller
app.controller('UserCtrl', function($scope, $routeParams) {
$scope.userId = $routeParams.id;
});
β Example URL:
http://localhost/#/user/42
π§Ύ This will render user.html
and set $scope.userId
to 42
.
π§ Real-World Use Case β Blog Post Navigation
.when('/post/:postId', {
templateUrl: 'views/post.html',
controller: 'PostCtrl'
});
app.controller('PostCtrl', function($scope, $routeParams, PostService) {
var id = $routeParams.postId;
$scope.post = PostService.getPost(id);
});
π« Handling Unknown Routes (404)
.otherwise({
template: '<h3>404 β Page Not Found</h3>'
});
β Ensures that users are shown a friendly message when they hit an unknown route.
π οΈ Best Practices
βοΈ Use ng-view
as a placeholder for route content
βοΈ Always configure otherwise()
to handle invalid URLs
βοΈ Modularize each routeβs controller and template
βοΈ Use services to fetch data in route controllers
βοΈ Use hashbang (#!
) URLs for older browser support
π Summary β Recap & Next Steps
AngularJS routing helps you build true single-page applications where users can navigate through multiple views without full page reloads. It’s simple to set up, easy to manage, and crucial for modern app structure.
π Key Takeaways:
- Use
$routeProvider
to define routes - Load templates dynamically with
ng-view
- Access parameters with
$routeParams
- Handle undefined routes with
otherwise()
βοΈ Real-world Relevance:
Used in SPAs like CRMs, blog systems, eCommerce dashboards, portfolio sites, admin panels, and interactive web portals.
β FAQ β AngularJS Routing
β What is ngRoute
used for in AngularJS?
β
It enables routing between different views without reloading the entire page.
β What does ng-view
do?
β
It serves as a placeholder in the HTML to load the template associated with the current route.
β How do you pass dynamic values in routes?
β
Use colon syntax like /user/:id
, and retrieve it using $routeParams
.
β How can I show a 404 page for unknown routes?
β
Use .otherwise()
in $routeProvider
to define a fallback template or redirect.
Share Now :