π§ AngularJS Navigation & Router Reference β Complete Guide for SPA Routing (2025)
π§² Introduction β Why Understand AngularJS Navigation & Router Reference?
In AngularJS Single Page Applications (SPAs), navigating between views without reloading the browser is crucial. AngularJS uses the ngRoute
module for routing and provides built-in support for programmatic navigation, hash-based URL tracking, deep linking, and view injection through <ng-view>
.
Understanding AngularJS navigation empowers developers to:
- Build seamless user experiences
- Navigate programmatically with
$location
- Use
$route
and$routeParams
for route-specific logic - Manage browser history effectively
π― In this guide, youβll learn:
- How AngularJS handles routing and navigation
- How to configure, control, and reference routes
- Useful APIs:
$route
,$location
,$routeParams
- Real-world navigation patterns and routing tips
π¦ Setting Up ngRoute in AngularJS
<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>
var app = angular.module("myApp", ["ngRoute"]);
π οΈ Basic Route Configuration
app.config(function($routeProvider) {
$routeProvider
.when("/home", {
templateUrl: "views/home.html",
controller: "HomeCtrl"
})
.when("/about", {
templateUrl: "views/about.html",
controller: "AboutCtrl"
})
.otherwise({
redirectTo: "/home"
});
});
π HTML Navigation with Hash URLs
<nav>
<a href="#!/home">Home</a>
<a href="#!/about">About</a>
</nav>
<!-- View outlet -->
<div ng-view></div>
β These anchor tags change the route without reloading the page.
π Programmatic Navigation using $location
AngularJS offers $location
service to navigate using JavaScript:
$scope.goToAbout = function() {
$location.path("/about");
};
π§Ύ You can call this on button click or any other user event.
π¬ Route Parameters via $routeParams
If you define a route like /user/:id
, use $routeParams
to access it:
$routeProvider
.when("/user/:id", {
templateUrl: "views/user.html",
controller: "UserCtrl"
});
app.controller("UserCtrl", function($scope, $routeParams) {
$scope.userId = $routeParams.id;
});
β
/user/42
will set userId
to 42
.
π§Ύ AngularJS Router API Reference
Service / Directive | Purpose |
---|---|
$routeProvider | Configure routes during app setup |
ng-view | Placeholder for dynamically injected view |
$location | Read and change the URL |
$routeParams | Access route parameters like :id |
$route | Get route-specific info and metadata |
π $location
Methods β URL Control
Method | Description |
---|---|
$location.path() | Get or set current path |
$location.search() | Read or write query parameters |
$location.url() | Full URL manipulation |
$location.hash() | Manage the hash fragment |
Example:
$location.path('/products').search({ category: 'books' });
β
Navigates to: #!/products?category=books
π $route
Object β Access Route Config
app.controller("Ctrl", function($scope, $route) {
console.log($route.current.templateUrl); // View being loaded
});
$route Property | Description |
---|---|
current | The currently active route config |
routes | All route definitions |
params | Route parameters passed in URL |
π¦ Real-World Use Case β Button-Based Navigation
<button ng-click="navigateTo('contact')">Contact Us</button>
$scope.navigateTo = function(view) {
$location.path('/' + view);
};
β
Navigates to /contact
using custom logic.
π οΈ Deep Linking with Bookmark Support
AngularJS enables deep linking via #!
in URLs:
<a href="#!/product/123">View Product</a>
- Bookmarkable
- Sharable
- Supports back/forward navigation
β Ideal for SPAs needing SEO-friendly or user-friendly links.
βοΈ Best Practices for AngularJS Navigation
βοΈ Use ng-view
for dynamic routing
βοΈ Use $location.path()
to navigate programmatically
βοΈ Always use named routes with :params
for scalability
βοΈ Sanitize $routeParams
before use
βοΈ Use otherwise()
to redirect unknown paths
π Summary β Recap & Next Steps
AngularJS offers a powerful and straightforward routing system ideal for SPAs. With built-in services like $location
, $routeParams
, and ng-view
, you can create dynamic, client-driven navigation flows with deep linking support.
π Key Takeaways:
- AngularJS uses
ngRoute
for SPA navigation - Use
ng-view
to load route-based templates - Navigate using anchor tags or
$location.path()
- Use
$routeParams
to access dynamic route variables
βοΈ Real-world Relevance:
Used in dashboards, portals, eCommerce apps, admin panels, and any app requiring SPA-style navigation.
β FAQ β AngularJS Navigation & Routing
β How do I change routes in AngularJS via code?
β
Use $location.path('/new-route')
to navigate programmatically.
β What is ng-view
in AngularJS?
β
Itβs a directive that serves as a placeholder for injecting templates based on the current route.
β Can I access query parameters in AngularJS routes?
β
Yes. Use $location.search()
to read or write query strings.
β How do I create a default route for unknown URLs?
β
Use .otherwise({ redirectTo: '/' })
in your $routeProvider
config.
Share Now :