AngularJS Routing

In this article, we will learn how to use Routing in AngularJS.

What is AngularJS Routing? We use routing when we want to navigate to different pages without reloading full application. In other words, we want the application to be a single page application with no page reloading.

You can use the ngRoute module. ngRoute module used to route application to different pages without reloading the full application.

Getting Started

First of all, install AngularJS and AngularJS.Route scripts in your application using Nuget Package Manager.


After installing as you can see there is one directory(Scripts) created in your application which has all installed scripts.


Or you can add references direct from googleapis.com like this.

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>

Now let’s add a new AngularJS Module using right click and click add new item and select AngularJS Module and give name and click Add.


Add define routes in angular module

  1. var app = angular.module("app", ["ngRoute"]);  
  2.   
  3. app.config(function ($routeProvider) {  
  4.   
  5. $routeProvider  
  6.   
  7. .when("/", {  
  8.   
  9. templateUrl: "index.html"  
  10.   
  11. })  
  12.   
  13. .when("/employee", {  
  14.   
  15. templateUrl: "employee.html"  
  16.   
  17. })  
  18.   
  19. .when("/customer", {  
  20.   
  21. templateUrl: "customer.html"  
  22.   
  23. })  
  24.   
  25. .when("/user", {  
  26.   
  27. templateUrl: "user.html"  
  28.   
  29. });  
  30.   
  31. });  
With the $routeProvider you can define what page to display when a user clicks a link. Define the $routeProvider using the config method of your application. Work registered in the config method will be performed when the application is loading.

 

Now let’s add a new HTML page and add reference of AngularJS and AngularJS Route scripts.

Add reference of module on HTML page

<script src="App/app.js"></script>


Test the application and click on Dashboard link. As you can see browser change with the name.


Click on customer dashboard


Work with Controller

Let’s check how to work with controller. First create a new controller using right click on Add new item and select controller and give appropriate name.


Let me post my controller’s code here

'customerCtrl'

  1. (function () {  
  2.   
  3. 'use strict';  
  4.   
  5. angular  
  6.   
  7. .module('app')  
  8.   
  9. .controller('customerCtrl', customer);  
  10.   
  11. customer.$inject = ['$scope'];  
  12.   
  13. function customer($scope) {  
  14.   
  15. $scope.title = 'This is customer dashoboard. here you can manage customer data.';  
  16.   
  17. activate();  
  18.   
  19. function activate() { }  
  20.   
  21. }  
  22.   
  23. })();  
'employeeCtrl'

 

  1. (function () {  
  2.   
  3. 'use strict';  
  4.   
  5. angular  
  6.   
  7. .module('app')  
  8.   
  9. .controller('employeeCtrl', employee);  
  10.   
  11. employee.$inject = ['$scope'];  
  12.   
  13. function employee($scope) {  
  14.   
  15. $scope.title = 'This is employee dashoboard. here you can manage employee data.';  
  16.   
  17. activate();  
  18.   
  19. function activate() { }  
  20.   
  21. }  
  22.   
  23. })();  
'userCtrl'

 

  1. (function () {  
  2.   
  3. 'use strict';  
  4.   
  5. angular  
  6.   
  7. .module('app')  
  8.   
  9. .controller('userCtrl', user);  
  10.   
  11. user.$inject = ['$scope'];  
  12.   
  13. function user($scope) {  
  14.   
  15. $scope.title = 'This is user dashoboard. here you can manage user data.';  
  16.   
  17. activate();  
  18.   
  19. function activate() { }  
  20.   
  21. }  
  22.   
  23. })();  
Now add controller name in module like this: 


Now add controller reference on HTML page.


Run the application


 

 

Conclusion

In this article we have learnt how to use routing in AngularJS and how to user controller with routing in AngularJS. If you have any question or comment, you can download attached sample or post me a comment in C# Corner comment section.

Up Next
    Ebook Download
    View all
    Learn
    View all