π§ AngularJS Nested, Dynamic & Wildcard Routes β Advanced Routing Techniques (2025 Guide)
π§² Introduction β Why Learn Advanced AngularJS Routing?
AngularJS’s routing system allows you to go far beyond basic navigation. With features like nested routes, dynamic parameters, and wildcard redirections, you can build flexible and scalable single-page applications (SPAs) that adapt to user roles, input, and unknown paths.
These routing capabilities are especially valuable for:
- Creating master-detail views
- Handling deep linking in dashboards
- Building modular and multi-level navigation systems
π― In this guide, youβll learn:
- How to configure nested routes using multiple views
- How to work with dynamic parameters (
$routeParams
) - How to set up wildcard routes for 404 handling or redirection
π§© AngularJS Routing Recap
AngularJS routing is configured using the ngRoute
module, which allows defining routes via $routeProvider
. Each route can:
- Load a specific template
- Bind a controller
- Accept route parameters
- Redirect or handle unmatched URLs
π οΈ Enable Routing Module
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-route.min.js"></script>
Declare the dependency:
angular.module('myApp', ['ngRoute']);
π§± 1. Nested Routing in AngularJS (Simulated)
While AngularJS does not support true nested routes natively like Angular (2+), you can simulate nesting using multiple ng-include
directives or nested ng-view
elements in combination with routing.
β Example: Master Template with Nested View Placeholder
Main Layout (index.html):
<body ng-app="myApp">
<div>
<h1>Admin Dashboard</h1>
<a href="#!/users">Users</a> |
<a href="#!/settings">Settings</a>
</div>
<div ng-view></div>
</body>
User View (users.html):
<h3>User List</h3>
<ul>
<li ng-repeat="user in users">
<a href="#!/users/{{user.id}}">{{ user.name }}</a>
</li>
</ul>
User Detail View (user-detail.html):
<h4>User Details</h4>
<p>Name: {{ user.name }}</p>
<p>Email: {{ user.email }}</p>
π§ 2. Dynamic Routing with Parameters
Define dynamic parameter in route:
.when('/users/:userId', {
templateUrl: 'views/user-detail.html',
controller: 'UserDetailCtrl'
})
Access it via $routeParams
:
app.controller('UserDetailCtrl', function($scope, $routeParams, UserService) {
var id = $routeParams.userId;
$scope.user = UserService.getById(id);
});
β Enables dynamic rendering of details based on URL.
𧨠3. Wildcard Routes β Catch All Unknown URLs
Use .otherwise()
or define a wildcard:
.otherwise({
redirectTo: '/not-found'
});
Or explicitly catch wildcard paths:
.when('/:path*', {
template: '<h2>404 β Page Not Found</h2>'
});
π§Ύ Ensures users see a fallback or error page for incorrect routes.
π§ Real-World Use Case β Dynamic Product Pages
.when('/products/:category/:productId', {
templateUrl: 'views/product-detail.html',
controller: 'ProductCtrl'
});
app.controller('ProductCtrl', function($scope, $routeParams) {
$scope.category = $routeParams.category;
$scope.productId = $routeParams.productId;
});
URL example:
http://localhost/#/products/electronics/234
β Useful for e-commerce, blog posts, or any content with nested identifiers.
π Simulating Nested Views with ng-include
Instead of ng-view
, you can manually include nested templates:
<div ng-include="'views/profile-main.html'"></div>
Then inside profile-main.html
:
<div>
<h2>Profile</h2>
<div ng-include="'views/profile-tabs.html'"></div>
</div>
β Offers flexibility without full nested routing support.
βοΈ Best Practices
βοΈ Use meaningful parameter names in dynamic routes
βοΈ Always validate and sanitize $routeParams
in controllers
βοΈ Use services to fetch route-based data cleanly
βοΈ Handle unknown routes with .otherwise()
βοΈ Modularize routes for large apps by feature
π Summary β Recap & Next Steps
Advanced routing in AngularJS using dynamic parameters, simulated nested views, and wildcard handling gives you the power to build feature-rich single-page apps. Whether you’re displaying user profiles, editing settings, or routing to blog content, these techniques are essential.
π Key Takeaways:
- Simulate nested routing with
ng-include
or nested views - Use
$routeParams
to fetch data based on dynamic routes - Handle 404s or catch-all cases with
otherwise()
or/:path*
- Dynamic URLs enable scalable, data-driven views
βοΈ Real-world Relevance:
Used in content management systems, admin panels, eCommerce platforms, dashboards, and multi-user systems.
β FAQ β AngularJS Nested, Dynamic & Wildcard Routes
β Does AngularJS support true nested routes?
β
Not natively. But you can simulate it using ng-include
or multiple templates loaded within a parent layout.
β How do I access dynamic route parameters?
β
Use $routeParams
in your controller to retrieve values like :id
, :slug
, etc.
β How do I handle unknown URLs or 404 errors?
β
Use .otherwise()
in $routeProvider
to redirect to a default route or display a custom template.
β Can I pass multiple parameters in a route?
β
Yes. Example:
.when('/product/:category/:id', {...})
Share Now :