AngularJS UI Custom Data

Overview

In this article, we will learn about AngularJS UI Custom Data, with an example. Here, we will add some custom data to our Controller. Let’s see how.

For more articles on AngularJS, pleasee refer to these links.

Introduction

To add the customData to a state, we use data property. So, let’s go back to our Controller and add a data property. Also, we will add some customData to our various states, like /home and /courses in our script.js file.
  1. .state("home", {  
  2.                         url: "/home",  
  3.                         templateUrl: "Templates/home.html",  
  4.                         controller: "homeController as homeCtrl",  
  5.                         data:{  
  6.                             customData1: "Home State Custom Data 1",  
  7.                             customData2:"Home Statae custom Data 2"  
  8.   
  9.                     }  
  10.                     })  
  11.   
  12.                       .state("courses", {  
  13.                           url: "/courses",  
  14.                           templateUrl: "Templates/courses.html",  
  15.                           controller: "coursesController as coursesCtrl",  
  16.                           data: {  
  17.                               customData1: "Courses State Custom Data 1",  
  18.                               customData2: "Courses Statae custom Data 2"  
  19.   
  20.                           }  
  21.   
  22.                       })  
Now, this customData is available for all the Controllers by default. Now, we want to retrieve this customData from our homeController and display this data to our homeView.

Now, let’s go to homeController function. To read the customData, we are going to use the state service.
  1. .controller("homeController"function ($state) {  
  2.                   this.message = "Home Page";  
  3.                   this.homeCustomData1 = $state.current.data.customData1;  
  4.               })  
So, we are accessing homeCustomData from the homeController function. So, when we use the current property, it’s going to return us the current state which is nothing but the home state and on that we use data property. Then, customData1 which is the name of the customData property and within that, JavaScript object which we had written earlier.

So, let’s do it for customData2 property,
  1. this.homeCustomData2 = $state.current.data.customData2;  
Now, from the same homeController function, I want to access the courses state data. Here, we will use a get function to get the courses state data.
  1. this.coursesCustomData1 = $state.get("courses").data.customData1;  
Get function is to get the courses state data, the data function, and the name of the property customData1.

Let’s do the same thing for CustomData2.
  1. this.coursesCustomData2 = $state.get("courses").data.customData2;  
So, our final script file 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) {  
  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("students", {  
  34.                                url: "/students",  
  35.                                templateUrl: "Templates/students.html",  
  36.                                controller: "studentsController as studentsCtrl",  
  37.                                resolve: {  
  38.                                    studentsList: function ($http) {  
  39.                                        return $http.get("StudentService.asmx/GetAllStudents")  
  40.                       .then(function (response) {  
  41.                           return response.data;  
  42.                       })  
  43.                                    }  
  44.   
  45.                                }  
  46.                            })  
  47.   
  48.                                        .state("studentDetails", {  
  49.                                            url: "/students/:id",  
  50.                                            templateUrl: "Templates/studentDetail.html",  
  51.                                            controller: "studentdetailcontroller as StudentDetailCtrl"  
  52.                                        })  
  53.   
  54.                         //              .when("/studentssearch/:name?", {  
  55.                         //                  templateurl: "templates/studentssearch.html",  
  56.                         //                  controller: "studentssearchcontroller as studentssearchctrl"  
  57.                         //              })  
  58.                         //    .otherwise({  
  59.                         //        redirectto: "/home"  
  60.                         //    })  
  61.                         //    $locationprovider.html5mode(true);  
  62.                     })  
  63.   
  64.                 .controller("homeController"function ($state) {  
  65.                     this.message = "Home Page";  
  66.                     this.homeCustomData1 = $state.current.data.customData1;  
  67.                     this.homeCustomData2 = $state.current.data.customData2;  
  68.                     this.coursesCustomData1 = $state.get("courses").data.customData1;  
  69.                     this.coursesCustomData2 = $state.get("courses").data.customData2;  
  70.                 })  
  71.   
  72.                  .controller("coursesController"function () {  
  73.                      this.courses = ["c#""SQL""Oracle"];  
  74.                  })  
  75.   
  76.                 .controller("studentsController"function (studentsList, $state, $location) {  
  77.                     var vm = this;  
  78.                     vm.searchStudents = function () {  
  79.                         if (vm.name) {  
  80.                             $location.url("/studentsSearch/" + vm.name);  
  81.                         }  
  82.                         else {  
  83.                             $location.url("/studentsSearch/");  
  84.                         }  
  85.   
  86.                     }  
  87.   
  88.   
  89.                     vm.reloadData = function () {  
  90.                         $state.reload();  
  91.                     }  
  92.                     vm.students = studentsList;  
  93.   
  94.                 })  
  95.   
  96.      .controller("studentdetailcontroller"function ($http, $stateParams) {  
  97.          var vm = this;  
  98.          $http({  
  99.              url: "StudentService.asmx/GetStudents",  
  100.              method: "get",  
  101.              params: {id:$stateParams.id}  
  102.          })  
  103.      .then(function (response) {  
  104.          vm.student = response.data;  
  105.      })  
  106.      })  
  107.   
  108. //.controller("studentsSearchController", function ($http, $routeParams) {  
  109. //    var vm = this;  
  110.   
  111. //    if ($routeParams.name) {  
  112. //        $http({  
  113. //            url: "StudentService.asmx/GetStudentsByName",  
  114. //            params: { name: $routeParams.name },  
  115.              
  116. //        })  
  117. //.then(function (response) {  
  118. //    vm.students = response.data;  
  119. //})  
  120. //    }  
  121. //    else {  
  122. //        $http.get("StudentService.asmx/GetAllStudents")  
  123. //                 .then(function (response) {  
  124. //                     vm.students = response.data;  
  125. //                 })  
  126. //    }  
Now, let’s go back to our home View and we will take two field sets to display customData as,
  1. <fieldset>  
  2.       <legend>Home  
  3.       </legend>  
  4.       CustomData1 :{{homeCtrl.homeCustomData1}} <br/>  
  5.       CustomData2 :{{homeCtrl.homeCustomData2}}  
  6.   </fieldset>  
We are using Controller as Syntax. So, we are binding that with the Controller as Syntax and the cutomData property for CutomData 1 and CustomData2 respectively.

Similarly, take another field set for courses.
  1. <fieldset>  
  2.     <legend>  
  3.         Courses  
  4.     </legend>  
  5.     CustomData1 :{{homeCtrl.coursesCustomData1}} <br />  
  6.     CustomData2 :{{homeCtrl.coursesCustomData2}}  
  7. </fieldset>  
So, our final home.html code is,
  1. <h1>{{homeCtrl.message}}</h1>  
  2. <div>  
  3.     In 1988, Microsoft joined Ashton-Tate and Sybase to create a variant of Sybase SQL Server for IBM OS/2 (then developed jointly with Microsoft), which was released the following year.[4] This was the first version of Microsoft SQL Server, and served as Microsoft's entry to the enterprise-level database market, competing against Oracle, IBM, and later, Sybase. SQL Server 4.2 was shipped in 1992, bundled with OS/2 version 1.3, followed by version 4.21 for Windows NT, released alongside Windows NT 3.1. SQL Server 6.0 was the first version designed for NT, and did not include any direction from Sybase.  
  4.     <ul>  
  5.         <li>The protocol layer implements the external interface to SQL Server. All operations that can be invoked on SQL Server are communicated to it via a Microsoft-defined format, called Tabular Data Stream (TDS). TDS is an application layer protocol, used to transfer data between a database server and a client. Initially designed and developed by Sybase Inc.</li>  
  6.     </ul>  
  7.     <fieldset>  
  8.         <legend>Home  
  9.         </legend>  
  10.         CustomData1 :{{homeCtrl.homeCustomData1}} <br/>  
  11.         CustomData2 :{{homeCtrl.homeCustomData2}}  
  12.     </fieldset>  
  13.   
  14.     <fieldset>  
  15.         <legend>  
  16.             Courses  
  17.         </legend>  
  18.         CustomData1 :{{homeCtrl.coursesCustomData1}} <br />  
  19.         CustomData2 :{{homeCtrl.coursesCustomData2}}  
  20.     </fieldset>  
  21. </div>  
Now, let’s save the changes and run the app. You will see the following.

page

You can see the home and courses custom data at the bottom, respectively .

To access the state custom data from its own Controller, we use - 

$state.current.data.customPropertyName

To access state custom data from a different Controller, we use - 

$state.get(“statename”).data.customPropertyName

Conclusion - So, this was all about AngularJS UI CustomData. Hope this article was helpful!!


Similar Articles