🧭 AngularJS Routing & Navigation
Estimated reading: 4 minutes 44 views

🧭 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 / DirectivePurpose
$routeProviderConfigure routes during app setup
ng-viewPlaceholder for dynamically injected view
$locationRead and change the URL
$routeParamsAccess route parameters like :id
$routeGet route-specific info and metadata

πŸ“˜ $location Methods – URL Control

MethodDescription
$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 PropertyDescription
currentThe currently active route config
routesAll route definitions
paramsRoute 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🧭 AngularJS Navigation & Router Reference

Or Copy Link

CONTENTS
Scroll to Top