Developing BookMySeat Application In AngularJS And ASP.NET - BookMySeat Page Layout , Angular Services And Custom Directive - Part Five

Here, I’ve thought to write my thoughts about hands-on Angular. This is the fifth article, which states how can you manage the BookMySeat layout and Angular Services.

This article elaborates about dealing with creating main bookmhtml page as well as Angular Services usage in order to populate values on the page.

Create a booking.html page under Application folder and paste the code segment, mentioned below in it, as depicted in the image, mentioned below.



If you notice in the screen, mentioned above, there is another file, routing.js, which keeps the complete logic written in the form of Angular controller as well as Angular Services.

Booking.html code snippet is mentioned below.

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.   
  5.     <!--<script src="../Scripts/angular.intellisense.js"></script>-->  
  6.   
  7.     <script data-require="ui-bootstrap@*" data-semver="0.10.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>  
  8.      <link href = "//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel = "stylesheet">  
  9.         
  10.      
  11.         <title></title>  
  12. </head>  
  13. <body ng-app="routingApp" class="container">  
  14.   
  15.     <div ng-controller="BookingController" align="center">  
  16.         <input type="text" name="name" value="" ng-model="onclick" />  
  17.         <br />  
  18.         <div>  
  19.            <b>UserName:</b>   <input type="text" ng-model="UserName"  style="background-color:yellow" ng-blur="ValidateUser(UserName)" ng-click="RemoveTag()"/>  
  20.              
  21.             <span ng-show="checkuser" style="color:green" >   
  22.                 <br />                  
  23.                <b>You have already booked your seat in SlotNo: {{SlotDetail}}</b>  
  24.             </span>  
  25.         </div>  
  26.         <!--<div>  
  27.             <span ng-show="">  
  28.   
  29.             </span>  
  30.         </div>-->  
  31.         <br />  
  32.         <br />  
  33.         <div class = "panel-body" >  
  34.         <div ng-init="testfunc()" >  
  35.             <span ng-repeat="image in images track by $index">  
  36.                 <span ng-if="(bookcollscope).indexOf($index)!=-1" >  
  37.                     <img id="{{$index}}" ng-src="{{imageUrl1}}" ng-mouseover="showOptionDetails($index)"/>  
  38.                     {{$index+1}}  
  39.                 </span>  
  40.                 <span ng-if="(bookcollscope).indexOf($index)==-1">  
  41.                     <img id="{{$index}}" ng-src="{{imageUrl}}" ng-click="BookMySeat(UserName,$index)" />  
  42.                     {{$index+1}}  
  43.                 </span>  
  44.                 <div ng-if="($index+1)%5==0">  
  45.                     <br></br>  
  46.                 </div>  
  47.             </span>  
  48.         </div>  
  49.             </div>  
  50.     </div>  
  51.      <dotnetpiper-directive></dotnetpiper-directive>  
  52. </body>  
  53. </html>  
Dependency injection

Dependency injection is shown below in the image.


BookingController plays an important role in this Application and is written in Routing.js file, shown in the image below.



Complete code snippet for BookingController has the complete logic for the data manipulation and Angular Services, as shown below.
  1. routingApp.controller("BookingController", ['$scope''dataService''$routeParams''$modal''$log'function ($scope, dataService, $routeParams, $modal, $log) {  
  2.     $scope.GetIndex = function (Index) {  
  3.         $scope.lastIndex = Index;  
  4.     }  
  5.     $scope.onclick = true;  
  6.     $scope.RemoveTag = function () {  
  7.        // alert("remove");  
  8.         $scope.checkuser = false;  
  9.     }  
  10.     var slotNum = $routeParams.slotNumber.substring(0, 3);  
  11.     var totalseat = 30;  
  12.     var ApiResult;  
  13.       
  14.     $scope.testfunc = function () {  
  15.         dataService.GetEmployeeService(slotNum).success(function (result) {  
  16.             $scope.empList = result;  
  17.             var imageCollection = new Array(totalseat);  
  18.             $scope.images = imageCollection;  
  19.             $scope.imageUrl = "../Images/RED.jpg";  
  20.             //   var bookedcollection = new Array($scope.empList);  
  21.             $scope.bookcollscope = $scope.empList;  
  22.             console.log($scope.bookcollscope);  
  23.             var imageCollection1 = new Array($scope.empList);  
  24.             $scope.images1 = imageCollection1;  
  25.             $scope.imageUrl1 = "../Images/GREEN.jpg";  
  26.         }).error(function (result) {  
  27.             $scope.error = true;  
  28.         });  
  29.     }  
  30.   
  31.     $scope.BookMySeat = function (UserName, SeatNo) {  
  32.         $scope.onclick=!$scope.onclick;  
  33.       
  34.         //$scope.$apply();  
  35.         console.log(UserName + SeatNo);  
  36.   
  37.         dataService.InsertSeat(UserName, slotNum, SeatNo).success(function (output) {  
  38.             //$scope.insertResult = output.data;  
  39.             $scope.empList = output;  
  40.             var imageCollection = new Array(totalseat);  
  41.             $scope.images = imageCollection;  
  42.             $scope.imageUrl = "../Images/RED.jpg";  
  43.             //   var bookedcollection = new Array($scope.empList);  
  44.             $scope.bookcollscope = $scope.empList;  
  45.             console.log($scope.bookcollscope);  
  46.             var imageCollection1 = new Array($scope.empList);  
  47.             $scope.images1 = imageCollection1;  
  48.             $scope.imageUrl1 = "../Images/GREEN.jpg";  
  49.             alert("Seat number " + SeatNo + " has been booked for : " + UserName);  
  50.         }).error(function (output) {  
  51.             $scope.error = true;  
  52.         });  
  53.     }  
  54.   
  55.     $scope.ValidateUser = function (UserName) {  
  56.         //alert("hello");  
  57.         dataService.ValidateUsername(UserName).success(function (output) {  
  58.             //alert(output);  
  59.             if (output != -1) {  
  60.                   
  61.                 $scope.checkuser = true;  
  62.                 $scope.SlotDetail = output;  
  63.             }  
  64.             console.log(output);  
  65.         }).error(function (output) {  
  66.             $scope.error = true;  
  67.         });  
  68.     }  
  69.     $scope.DeleteBooking = function (SeatNo) {  
  70.   
  71.         dataService.DeleteSeat(SeatNo, slotNum).success(function (output) {  
  72.             console.log(output);  
  73.         })  
  74.     .error(function (output) {  
  75.         $scope.error = true;  
  76.     });  
  77.     }  
  78.     $scope.option = {  
  79.         name: 'Name (hover over for more details)',  
  80.         longDescription: 'This is my detailed description...  lots of text here'  
  81.     }  
  82.     $scope.showOptionDetails = function (SeatNo) {  
  83.         dataService.GetSeatDetail(slotNum, SeatNo).success(function (output) {  
  84.             console.log("seat name is " + output);  
  85.             $log.info($scope.option);  
  86.             $scope.optionModal = $modal.open({  
  87.                 template: '<div class="modal-header" style=" background-color: red;" ng-mouseleave="close()"><h3 class="modal-title">Seat Details </h3></div><div class="modal-body"style=" background-color: yellow;" ><b>UserName:</b> ' + output + '   <b> SeatNo: </b> {{' + (SeatNo + 1) + '}}</div><div class="modal-footer" ng-click="close()"><input type="button" ng-click="DeleteBooking(' + SeatNo + ')" value="Delete"/>Close</div>',  
  88.                 controller: 'modalCtrl',  
  89.                 resolve: {  
  90.                     option: function () {  
  91.                         return $scope.option;  
  92.                     }  
  93.                 }  
  94.             });  
  95.         }).error(function (output) {  
  96.             $scope.error = true;  
  97.         });  
  98.     }  
  99.     $scope.closeOptionDetails = function () {  
  100.         $scope.optionModal.close();  
  101.     }  
  102. }]).directive('dotnetpiperDirective'function () {  
  103.     var directive = {};  
  104.     directive.restrict = 'E'/* restrict this directive to elements */  
  105.     directive.templateUrl = "Application/showBookingData.html";    
  106.       
  107.     return directive;  
  108. });  
If you notice the code segment, mentioned above, then you will find that there is a custom directive with dotnetpiperDirective placed in bookingcontroller and also placed in booking.html.


Angular Services

Services are a point, where you can put common functionality to an AngularJS Application. For example, if you would like to share the data with more than one controller then the best way is to promote the data to the Service and then make it available via the Service. Services extend the controllers and make them more globally accessible.

This is the complete code snippet used in in this application, there are six methods to perform different operations. Kindly visit this link to understand Web API methods.

This is how Angular Service is defined, as shown below in the code segment.


Code Segment

  1. routingApp.service('dataService', ['$http''$window'function ($http, $window) {  
  2.     var result;  
  3.     this.GetEmployeeService = function (slotNum) {  
  4.         result = $http.get('api/BookMySeatAPI/GetSeatCount?slot=' + slotNum).success(function (data, status) {  
  5.             result = data.result;  
  6.         }).error(function (error) {  
  7.             result = error;  
  8.             alert("Something happned wrong");  
  9.         });  
  10.         console.log(result);  
  11.         return result;  
  12.     }  
  13.     this.GetShowbookingDetailService = function () {  
  14.         result = $http.get('api/BookMySeatAPI/ShowBookSeat').success(function (data, status) {  
  15.             result = data.result;  
  16.         }).error(function (error) {  
  17.             result = error;  
  18.             alert("Something happned wrong");  
  19.         });  
  20.         console.log(result);  
  21.         return result;  
  22.     }  
  23.     this.InsertSeat = function (UserName, slotNum, SeatNo) {  
  24.         var resultPost;  
  25.         var Indata = { 'UserName': UserName, 'TimeSlot': slotNum, 'SeatNo': SeatNo };  
  26.         return this.resultPost = $http.post('api/seat/BookMySeatAPI/SeatBook', Indata).success(function (data) {  
  27.             this.resultPost = data.result;  
  28.             $window.location.reload();  
  29.             console.log(resultPost);  
  30.         }).error(function (error) {  
  31.             this.resultPost = error;  
  32.             alert("Value thrown @ iNSERT FUNCTION");  
  33.         });  
  34.         // return resultPost;  
  35.     }  
  36.     this.GetSeatDetail = function (slotNum, SeatNo) {  
  37.         var resultSeatDetail;  
  38.         var Indata = { 'TimeSlot': slotNum, 'SeatNo': SeatNo };  
  39.         return this.resultSeatDetail = $http.post('api/seatdetail/BookMySeatAPI/SeatDetail', Indata).success(function (data) {  
  40.   
  41.             console.log(data);  
  42.         }).error(function (error) {  
  43.             this.resultSeatDetail = error;  
  44.   
  45.         });  
  46.     }  
  47.     this.ValidateUsername = function (UserName) {  
  48.         var resultSeatDetail;  
  49.         var Indata = { 'UserName': UserName };  
  50.         return this.resultSeatDetail = $http.post('api/ValidateUser/BookMySeatAPI/ValidateUser', Indata).success(function (data) {  
  51.   
  52.             console.log(data);  
  53.         }).error(function (error) {  
  54.             this.resultSeatDetail = error;  
  55.   
  56.         });  
  57.     }  
  58.     this.DeleteSeat = function (SeatNo, SlotNo) {  
  59.         var resultSeatDetail;  
  60.         console.log(SeatNo + '' + SlotNo);  
  61.         var Indata = { 'SeatNo': SeatNo, 'TimeSlot': SlotNo };  
  62.         return this.resultSeatDetail = $http.post('api/DeleteSeat/BookMySeatAPI/DeleteSeat', Indata).success(function (data) {  
  63.             $window.location.reload();  
  64.             console.log(data);  
  65.         }).error(function (error) {  
  66.             this.resultSeatDetail = error;  
  67.   
  68.         });  
  69.     }  
  70.     this.SaveEmployee = function (employee) {  
  71.         var result;  
  72.         //alert("service called");  
  73.         result = $http.post('/Employee/SaveEmployee', employee).success(function (data, status) {  
  74.             alert(data);  
  75.             result = data;  
  76.             console.log(data);  
  77.             //$scope.success = true;  
  78.         }).error(function (error) {  
  79.             result = error;  
  80.         });  
  81.         return result;  
  82.     }  
  83. }]);  
Note

There are certain areas where the code can be improved. Hence, please look into those factors, which will be beneficial for an individual.

Hope it’ll help you some day. Enjoy coding. 

Up Next
    Ebook Download
    View all
    Learn
    View all