AngularJS UI-Router Multiple Named Views

In this article, you will learn about AngularJS UI-Router multiple named Views.

Overview

In this article, we will learn about multiple named Views in detail, with an example. So, let’s start.

For more articles on AngularJS, refer to these links.

Introduction

One of the benefits of using UI-Router over ng-route is that UI-Router supports multiple named Views. Let’s understand the multiple named Views. We will continue with the same example which we had carried out for displaying the total number of females, males, and list of students in article AngularJS UI-Router Nested Views.

Right now, when we click on details of students, our current output is -



Here, I want to display only the total number of males and females in that. I don’t want to display the total number of students. We are going to solve this problem by using multiple named Views.

First, we will create two named Views. Right now, our studentparent.html page contains Views tag as-



Now, we add two divs and one will display the parent view and other will display the child view as,


The first div will be responsible for displaying parent View details and the second div will be responsible for displaying child View details.

The next step is to modify our studentParent.student state in our Controller file.

We will write Views and then pass the child View as student Data.
  1. .state("studentParent.students", {  
  2.     url: "/",  
  3.     views:{  
  4.         "studentData" : {  
  5.   
  6.             templateUrl: "Templates/students.html",  
  7.             controller: "studentsController as studentsCtrl",  
  8.             resolve: {  
  9.                 studentsList: function ($http) {  
  10.                     return $http.get("StudentService.asmx/GetAllStudents")  
  11.    .then(function (response) {  
  12.        return response.data;  
  13.    })  
  14.         }  
  15.     }  
  16.   
  17.         }  
  18.   
  19.     }  
  20. })  
We have another View, totalData View, that our Controller page is going to display. We will make use of this totalData named View. So, add these lines in the Controller.



We don’t have studentstotal.html and studentstotal Controller. So, we will create that first.

This our studentsController.
  1. .controller("studentsTotalController"function (studentTotals) {  
  2.    this.total = studentTotals.total;
  3.    })  
Notice that we are injecting students total which is coming from the parent state so if you look our state as -
.state("studentParent.students", {

For student, the parent is studentParent and within studentparent, we have resolved the dependency as studentsTotals. So, this can be passed safely to all the child Controller functions.



And studentsTotal Controller function is the child Controller.


Now, the next step is to create a View Template called studentsTotal.html. Add an html file in templates folder,



As we are using Controller as syntax, we bound that with studentsTotalctrl. Here, we want to display totals respectively.

The next step is to modify studentdetailscontroller as,


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", ["ui.router"])  
  5.                     .config(function ($stateProvider, $urlMatcherFactoryProvider, $urlRouterProvider,$locationProvider) {  
  6.                         //  $routeProvider.caseInsensitiveMatch = true;  
  7.                         $urlRouterProvider.otherwise("/home");  
  8.                         $urlMatcherFactoryProvider.caseInsensitive(true);  
  9.                         $stateProvider  
  10.                         .state("home", {  
  11.                             url: "/home",  
  12.                             templateUrl: "Templates/home.html",  
  13.                             controller: "homeController as homeCtrl",  
  14.                             data:{  
  15.                                 customData1: "Home State Custom Data 1",  
  16.                                 customData2:"Home Statae custom Data 2"  
  17.   
  18.                         }  
  19.                         })  
  20.   
  21.                           .state("courses", {  
  22.                               url: "/courses",  
  23.                               templateUrl: "Templates/courses.html",  
  24.                               controller: "coursesController as coursesCtrl",  
  25.                               data: {  
  26.                                   customData1: "Courses State Custom Data 1",  
  27.                                   customData2: "Courses Statae custom Data 2"  
  28.   
  29.                               }  
  30.   
  31.                           })  
  32.   
  33.                             .state("studentParent", {  
  34.                                 url: "/students/",  
  35.                                 controller: "studentParentController",  
  36.                                 controllerAs: "stdParentCtrl",  
  37.                                 templateUrl: "Templates/stduentParent.html",  
  38.                                 resolve: {  
  39.                                     studentTotals: function ($http) {  
  40.                                         return $http.get("StudentService.asmx/GetStudentTotals")  
  41.                                         .then(function (response) {  
  42.                                             return response.data;  
  43.   
  44.                                         })  
  45.                                     }  
  46.   
  47.                                 },  
  48.                                 abstracttrue  
  49.                             })  
  50.   
  51.   
  52.   
  53.   
  54.                            .state("studentParent.students", {  
  55.                                url: "/",  
  56.                                views:{  
  57.                                    "studentData" : {  
  58.   
  59.                                        templateUrl: "Templates/students.html",  
  60.                                        controller: "studentsController as studentsCtrl",  
  61.                                        resolve: {  
  62.                                            studentsList: function ($http) {  
  63.                                                return $http.get("StudentService.asmx/GetAllStudents")  
  64.                               .then(function (response) {  
  65.                                   return response.data;  
  66.                               })  
  67.                                    }  
  68.                                }  
  69.                      
  70.                                    },  
  71.                                    "totalData": {  
  72.   
  73.                                        templateUrl: "Templates/studentsTotal.html",  
  74.                                        controller: "studentsTotalController as studentsTotalCtrl",  
  75.                                    }  
  76.   
  77.                                }  
  78.                            })  
  79.   
  80.                                        .state("studentParent.studentDetails", {  
  81.                                            url: "/:id",  
  82.                                            views:{  
  83.   
  84.                                                "studentData": {  
  85.                                                    templateUrl: "Templates/studentDetail.html",  
  86.                                                    controller: "studentdetailcontroller as StudentDetailCtrl"  
  87.                                                }  
  88.                                            }  
  89.                                            
  90.                                        })  
  91.   
  92.                         //              .when("/studentssearch/:name?", {  
  93.                         //                  templateurl: "templates/studentssearch.html",  
  94.                         //                  controller: "studentssearchcontroller as studentssearchctrl"  
  95.                         //              })  
  96.                         //    .otherwise({  
  97.                         //        redirectto: "/home"  
  98.                         //    })  
  99.                         $locationProvider.html5Mode(true);  
  100.                     })  
  101.   
  102.                 .controller("homeController"function ($state) {  
  103.                     this.message = "Home Page";  
  104.                     this.homeCustomData1 = $state.current.data.customData1;  
  105.                     this.homeCustomData2 = $state.current.data.customData2;  
  106.                     this.coursesCustomData1 = $state.get("courses").data.customData1;  
  107.                     this.coursesCustomData2 = $state.get("courses").data.customData2;  
  108.                 })  
  109.   
  110.                  .controller("coursesController"function () {  
  111.                      this.courses = ["c#""SQL""Oracle"];  
  112.                  })  
  113.   
  114.                 .controller("studentsController"function (studentsList, $state, $location,studentTotals) {  
  115.                     var vm = this;  
  116.                     vm.searchStudents = function () {  
  117.                         if (vm.name) {  
  118.                             $location.url("/studentsSearch/" + vm.name);  
  119.                         }  
  120.                         else {  
  121.                             $location.url("/studentsSearch/");  
  122.                         }  
  123.   
  124.                     }  
  125.   
  126.   
  127.                     vm.reloadData = function () {  
  128.                         $state.reload();  
  129.                     }  
  130.                     vm.students = studentsList;  
  131.                     vm.studentTotals = studentTotals;  
  132.   
  133.                 })  
  134.   
  135.     .controller("studentParentController"function (studentTotals) {  
  136.   
  137.         this.males = studentTotals.males;  
  138.         this.females = studentTotals.females;  
  139.         this.total = studentTotals.total;  
  140.   
  141.   
  142.     })  
  143.   
  144.       .controller("studentsTotalController"function (studentTotals) {  
  145.   
  146.          
  147.           this.total = studentTotals.total;  
  148.   
  149.   
  150.       })  
  151.   
  152.      .controller("studentdetailcontroller"function ($http, $stateParams) {  
  153.          var vm = this;  
  154.          $http({  
  155.              url: "StudentService.asmx/GetStudents",  
  156.              method: "get",  
  157.              params: {id:$stateParams.id}  
  158.          })  
  159.      .then(function (response) {  
  160.          vm.student = response.data;  
  161.      })  
  162.      })  
  163.   
  164. //.controller("studentsSearchController", function ($http, $routeParams) {  
  165. //    var vm = this;  
  166.   
  167. //    if ($routeParams.name) {  
  168. //        $http({  
  169. //            url: "StudentService.asmx/GetStudentsByName",  
  170. //            params: { name: $routeParams.name },  
  171.              
  172. //        })  
  173. //.then(function (response) {  
  174. //    vm.students = response.data;  
  175. //})  
  176. //    }  
  177. //    else {  
  178. //        $http.get("StudentService.asmx/GetAllStudents")  
  179. //                 .then(function (response) {  
  180. //                     vm.students = response.data;  
  181. //                 })  
  182. //    }  
Save the changes and run the app.

When you click on students View, you will get the following output.



When you click on any student, you get the output, as shown below.



Conclusion - This was all about UI-Router multiple Views. Hope this article was helpful!!