AngularJS Route Resolve

 Overview

In this article, we will see AngularJS Route Resolve and its various problems, with an example. We will work with the same example which we were working with in earlier articles' demo applications.

For more articles on AngularJS, refer these links.

Introduction

As we are continuing with the same example, here is the list of students.

output

Here, the database and application are on same machine. So, the data fetched from our web service is very fast. What if these are residing on different servers ? We will check the data transfer rate. Here, we will use some network latency to do that, using thread. Sleep in your web service as,
code

Let’s run the Solution.

output

Initially, when I clicked on Student Controller, the data didn’t appear because we have written thread.sleep in our web method. Also, you can look at the Controller where we have written a promise function. When that promise is resolved, it will load the data.

code

After a certain interval, we get the list of students.

Why? Because it has resolved the promise function and displayed the list of students.

output

Now, as our data is fetched asynchronously, we will write a resolve in our student’s Controller, and will write a function that will have a promise.
  1. .when("/students", {  
  2.                   templateUrl: "Templates/students.html",  
  3.                   controller: "studentsController as studentsCtrl",  
  4.                   resolve: {  
  5.                       studentsList: function ($http) {  
  6.                        return   $http.get("StudentService.asmx/GetAllStudents")  
  7.       .then(function (response) {  
  8.           return response.data;  
  9.       })  
Now, look at the studentsController. Here, we have used a resolve and assigned the resolve to students List. The function had a promise. So, when that promise is resolved by the route, it will load the data.
 
So, we removed that variable vm which we did use earlier. Here, when the promise is resolved, it will respond to that data which is coming from the web service method we used in our web service.

Now, let's move to the next change. As this is coming from studentsController, we need to assign those in the parameter which we used in studentsList, and remove $http too as we are resolving those requests.
  1. .controller("studentsController"function (studentsList, $route, $location) {  
Now, assign it to the vm variable.
  1. vm.students = studentsList;  
So, our final Controller code is.
  1. /// <reference path="angular.min.js" />  
  2. /// <reference path="angular-route.min.js" />  
  3.   
  4. var app = angular.module("Demo", ["ngRoute"])  
  5.                     .config(function ($routeProvider, $locationProvider) {  
  6.                         $routeProvider.caseInsensitiveMatch = true;  
  7.   
  8.                         $routeProvider  
  9.   
  10.                         .when("/home", {  
  11.                             template: "<h1>Hello There !!</h1>",  
  12.                             controller: "homeController as homeCtrl"  
  13.                         })  
  14.   
  15.                           .when("/courses", {  
  16.                               templateUrl: "Templates/courses.html",  
  17.                               controller: "coursesController as coursesCtrl",  
  18.                                
  19.                           })  
  20.   
  21.                            .when("/students", {  
  22.                                templateUrl: "Templates/students.html",  
  23.                                controller: "studentsController as studentsCtrl",  
  24.                                resolve: {  
  25.                                    studentsList: function ($http) {  
  26.                                     return   $http.get("StudentService.asmx/GetAllStudents")  
  27.                    .then(function (response) {  
  28.                        return response.data;  
  29.                    })  
  30.                                    }  
  31.   
  32.                                }  
  33.                            })  
  34.   
  35.                                 .when("/students/:id", {  
  36.                                     templateUrl: "Templates/StudentDetail.html",  
  37.                                     controller: "StudentDetailController as StudentDetailCtrl"  
  38.                                 })  
  39.   
  40.                                   .when("/studentsSearch/:name?", {  
  41.                                       templateUrl: "Templates/studentsSearch.html",  
  42.                                       controller: "studentsSearchController as studentsSearchCtrl"  
  43.                                   })  
  44.                         .otherwise({  
  45.                             redirectTo: "/home"  
  46.                         })  
  47.                         $locationProvider.html5Mode(true);  
  48.                     })  
  49.   
  50.                 .controller("homeController"function () {  
  51.                     this.message = "Home Page";  
  52.                 })  
  53.   
  54.                  .controller("coursesController"function () {  
  55.                      this.courses = ["c#""SQL""Oracle"];  
  56.                  })  
  57.   
  58.                 .controller("studentsController"function (studentsList, $route, $location) {  
  59.                     var vm = this;  
  60.                     vm.searchStudents = function () {  
  61.                         if (vm.name) {  
  62.                             $location.url("/studentsSearch/" + vm.name);  
  63.                         }  
  64.                         else {  
  65.                             $location.url("/studentsSearch/" );  
  66.                         }  
  67.   
  68.                     }  
  69.                 
  70.   
  71.                     vm.reloadData = function () {  
  72.                         $route.reload();  
  73.                     }  
  74.                     vm.students = studentsList;  
  75.                    
  76.                       })  
  77.       .controller("StudentDetailController"function ( $http, $routeParams) {  
  78.           var vm = this;  
  79.           $http({  
  80.           url: "StudentService.asmx/GetStudents",  
  81.           params: { id: $routeParams.id },  
  82.           method:"get"  
  83.       })  
  84.       .then(function (response) {  
  85.           vm.student = response.data;  
  86.       })  
  87.       })  
  88.   
  89. .controller("studentsSearchController"function ($http, $routeParams) {  
  90.     var vm = this;  
  91.   
  92.     if ($routeParams.name) {  
  93.         $http({  
  94.             url: "StudentService.asmx/GetStudentsByName",  
  95.             params: { name: $routeParams.name },  
  96.              
  97.         })  
  98. .then(function (response) {  
  99.     vm.students = response.data;  
  100. })  
  101.     }  
  102.     else {  
  103.         $http.get("StudentService.asmx/GetAllStudents")  
  104.                  .then(function (response) {  
  105.                      vm.students = response.data;  
  106.                  })  
  107.     }  
  108.       
  109. });  
Let’s save the changes and run the application.

output

output

Initially, we are on the home Controller. Let's click on the studentsController and see what happens.

output

When I click on the studentsController, look at the URL (above screenshot). It shows students but the data is not loaded. When a promise is resolved, it requests to the service and after certain time, the data is loaded.

NOTE 
  • So, the Resolve property contains one or more promises that must resolve successfully, before transitioning to the new route.
  • The property names used in the resolve property can then be injected into the Controller. This means the Controller does not have to fetch the data.

output

Output


output

Conclusion

So, this was all about Route Resolve in AngularJS. Hope this article was helpful!!!


Similar Articles