Responsive Modal Directive In AngularJS With Bootstrap

As per today’s web development, responsive pattern is the most import pattern for developing any web site or web application since today many users want to access those websites or applications not only in desktop computers, they also want to access those things in mobile, tab or other small devices. So becasue of this it is important to design our page in such a way so that the page acts correctly in all devices. The same concept needs to apply when we want to open any modal popup from our pages. Since in a normal scenario modal is actually rendered as a html div with a specific height and width,  it is very important to design our html or css in such a way that it work perfectly on  all devices.

So, in this article, we will try to create an AngularJS directive for modal which is responsive. Since modal window meansthe developer needs to change the content of that window from page to page, for this reason, we use ng-transclude option for include html contents within the modal window. Required html code is needed to write in the page with the angular directive.

For this, we first add css file named style.css  and write down the below code:
  1. .animated {  
  2.     -webkit-animation-duration: 1s;  
  3.     animation-duration: 1s;  
  4.     -webkit-animation-fill-mode: both;  
  5.     animation-fill-mode: both;  
  6.     z-index100;  
  7. }  
  8. .modal-backdrop { z-index2040 !important; }  
  9. .modal { z-index2050 !important; }  
  10. body.modal-open { padding-right: inherit !important; }  
  11. body.modal-open .animated { animation-fill-mode: initial; }  
  12. .modal-content .panel {margin-bottom:0;}  
Now, add a javascript file named sampleDirective.js and write down the below code from modal directive:
  1. 'use strict';  
  2.   
  3. var testApp = angular.module('TestApp', []);  
  4. testApp.directive('ngModal', ['$http''$timeout'function () {  
  5.     return {  
  6.         restrict: 'E',  
  7.         transclude: true,  
  8.         scope: {  
  9.         },  
  10.         bindToController: {  
  11.             param: '=',  
  12.         },  
  13.         template: '<div class="modal inmodal" id="{{model.param.controlId}}" tabindex="-1" role="dialog" aria-hidden="true" data-ng-style="{display : model.display}">' +  
  14.                     '    <div class="modal-dialog">' +  
  15.                     '        <div class="modal-content animated bounceInRight">' +  
  16.                     '            <div class="panel panel-primary">' +  
  17.                     '                <div class="panel-heading">{{model.param.caption}}' +  
  18.                     '                    <button type="button" class="close" data-dismiss="modal" data-ng-click="model.fnClose();"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' +  
  19.                     '                </div>' +  
  20.                     '                <div class="panel-body" ng-transclude>' +  
  21.                     '                </div>' +  
  22.                     '            </div>' +  
  23.                     '        </div>' +  
  24.                     '    </div>' +  
  25.                     '</div>' +  
  26.                    '<div class="modal-backdrop in" data-ng-show="model.showModal"></div>',  
  27.         controllerAs: 'model',  
  28.         controller: function ($http, $timeout) {  
  29.             var self = this;  
  30.   
  31.             if (self.param == undefined) {  
  32.                 self.param = {};  
  33.             }  
  34.   
  35.             self.display = 'none';  
  36.   
  37.             self.fnClose = function () {  
  38.                 self.showModal = false;  
  39.                 self.display = 'none';  
  40.             }  
  41.   
  42.             function assignModalMethod() {  
  43.                 self.param.method = {  
  44.                     show: function () {  
  45.                         self.showModal = true;  
  46.                         self.display = 'block';  
  47.                     },  
  48.                     close: function () {  
  49.                         self.fnClose();  
  50.                     },  
  51.                     setTitle: function (args) {  
  52.                         self.param.caption = args;  
  53.                     }  
  54.                 }  
  55.             }  
  56.   
  57.             var initModalEvent = function () {  
  58.                 $('#' + self.param.controlId).keyup(function (e) {  
  59.                     if (e.which == 27) {  
  60.                         $timeout(function () {  
  61.                             self.fnClose();  
  62.                         }, 50);  
  63.                     }  
  64.                 });  
  65.             }  
  66.   
  67.             assignModalMethod();  
  68.             $timeout(function () {  
  69.                 initModalEvent();  
  70.             }, 100);  
  71.         }  
  72.     }  
  73. }]);  
Now, add another javascript file with name indexcontroller.js and write down the code for controller page:
  1. testApp.controller('IndexController', ['$http'function ($http, urlService) {  
  2.     var self = this;  
  3.   
  4.     self.message = 'Modal';  
  5.   
  6.     self.modalArgs = {  
  7.         caption: 'Modal Window',  
  8.         controlId: 'modal1'  
  9.     };  
  10.   
  11.     self.showModal = function () {  
  12.         self.modalArgs.method.show();  
  13.     }  
  14.   
  15.     self.hideModal = function () {  
  16.         self.modalArgs.method.close();  
  17.     }  
  18. }]);  
Now, add a html file and write down the below code:
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>Modal</title>  
  5.     <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />  
  6.     <link href="style.css" rel="stylesheet" />  
  7.     <script src="../../RefScript/jquery-2.1.1.js"></script>  
  8.     <script src="../../Scripts/angular.min.js"></script>  
  9.     <script src="SampleDirective.js"></script>  
  10.     <script src="IndexController.js"></script>  
  11.   
  12. </head>  
  13. <body data-ng-app="TestApp" data-ng-controller="IndexController as model">  
  14.     <div class="row animated fadeInRight">  
  15.         <div class="col-lg-12">  
  16.             <div class="rowDiv">  
  17.                 <h3>{{model.message}}</h3>  
  18.             </div>  
  19.         </div>  
  20.     </div>  
  21.     <div class="actionBtn hideMini">  
  22.         <div class="row">  
  23.             <input type="button" class="btn-default active" value="Modal Show" data-ng-click="model.showModal();" />  
  24.         </div>  
  25.     </div>  
  26.     <ng-modal param="model.modalArgs">  
  27.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.</p>  
  28.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.</p>  
  29.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.</p>  
  30.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.</p>  
  31.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.</p>  
  32.   
  33.         <input type="button" class="btn-default active" value="Modal Close" data-ng-click="model.hideModal();" />  
  34.   
  35.     </ng-modal>  
  36. </body>  
  37. </html>  
Now, run the code and the output will be as below:
 
Now, you can the browser size and check the same output again. 
 
Read more articles on AngularJS:
 


Similar Articles