When we want to develop a web-based application using AngularJs, it often requires a modal dialog box control with some extra features like height, width, title and so on. But like classic ASP.NET, a ready-made control is not always available in the AngularJs framework. If we want to create a custom modal dialog control, we can create it easily. Using this article, we will learn how to create a custom modal dialog control.
For that, we will first open Visual Studio and create a blank ASP.NET web application. We then need to install some NuGet packages such as AngularJs, Bootstrap and AngularUI.


Figure 1: AngularJs UI Bootstrap
First we will create a modal directory with the attributes. For this, create an HTML file named modal.html and add the following code to that file:
  1. <div class="modal-dialog" ng-show="showModal">
  2. <div class="modal-backdrop"></div>
  3. <div class="modal-content" >"height:{{height}};width:{{width}};left:{{left}};top:{{top}};">
  4. <div class="modal-header">
  5. <button type="button" class="close" data-dismiss="modal" aria-hidden="true" ng-click="close();" ng-hide="HideCloseButton">×</button>
  6. <h4>{{ModalParam.title}}</h4>
  7. </div>
  8. <div class="modal-body" ng-transclude >"height:{{BodyHeight}};">
  9. </div>
  10. </div>
  11. </div>
First, we need to define the Angular app as in the following:
  1. var CustomCtrlApp = angular.module('CustomCtrlApp', ['ui.bootstrap']);
In the script folder, add a JavaScript file named Modal.js that is basically the controller file of the modaldirectory. In the JavaScript file, add the following code:
  1. CustomCtrlApp.directive('modal', function () {
  2. return {
  3. restrict: 'EA',
  4. scope: {
  5. ModalParam: '=modalSetting'
  6. },
  7. transclude: true,
  8. replace: true,
  9. templateUrl: '../HTMLTemplate/Modal.html',
  10. controller: function ($scope, $element, $attrs) {
  11. $scope.showModal = false;
  12. if ($scope.ModalParam == undefined) {
  13. $scope.ModalParam = {};
  14. }
  15. if ($scope.ModalParam.width == undefined) {
  16. $scope.width = '500px'
  17. $scope.ModalParam.width = 500;
  18. }
  19. else {
  20. if ($scope.ModalParam.width > 1000) {
  21. $scope.ModalParam.width = 1000;
  22. }
  23. $scope.width = $scope.ModalParam.width + 'px';
  24. }
  25. if ($scope.ModalParam.height == undefined) {
  26. $scope.height = '400px'
  27. $scope.ModalParam.height = 400;
  28. }
  29. else {
  30. if ($scope.ModalParam.height > 900) {
  31. $scope.ModalParam.height = 900;
  32. }
  33. $scope.height = $scope.ModalParam.height + 'px';
  34. }
  35. if ($scope.ModalParam.showCloseButton == undefined) {
  36. $scope.ModalParam.showCloseButton = true;
  37. }
  38. if ($scope.ModalParam.showCloseButton == true) {
  39. $scope.HideCloseButton = false;
  40. }
  41. else {
  42. $scope.HideCloseButton = true;
  43. }
  44. $scope.left = Math.round((screen.availWidth - $scope.ModalParam.width) / 2, 0) + 'px';
  45. $scope.top = Math.round((screen.availHeight - $scope.ModalParam.height - 20) / 2, 0) + 'px';
  46. $scope.BodyHeight = ($scope.ModalParam.height - 40) + 'px';
  47. function assignModalMethod() {
  48. $scope.ModalParam.method = {
  49. show: function () {
  50. $scope.show();
  51. },
  52. close: function () {
  53. $scope.close();
  54. },
  55. setTitle: function (args) {
  56. $scope.ModalParam.Title = args;
  57. }
  58. }
  59. }
  60. $scope.show = function () {
  61. $scope.showModal = true;
  62. }
  63. $scope.close = function () {
  64. $scope.showModal = false;
  65. }
  66. assignModalMethod();
  67. }
  68. };
  69. });
Next, add an HTML page in the HTML folder named ModalDemo.html and add the following code there:
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Model Demo</title>
  5. <script src="../RefScript/angular.min.js"></script>
  6. <script src="../RefScript/ui-bootstrap.min.js"></script>
  7. <script src="../PageScript/CustomCtrlApp.js"></script>
  8. <script src="../DirectiveScript/Modal.js"></script>
  9. <script src="../PageScript/ModalDemo.js"></script>
  10. <link href="../Ref/bootstrap.min.css" rel= />
  11. < type="text/css">
  12. .modal-backdrop {
  13. opacity: 0.6;
  14. }
  15. .modal-content {
  16. position: fixed;
  17. z-index: 1041;
  18. border-radius: 0;
  19. }
  20. .modal-header {
  21. padding: 0px 10px;
  22. }
  23. .modal-body {
  24. overflow-y: auto;
  25. overflow-x: hidden;
  26. }
  27. </>
  28. </head>
  29. <body ng-app="CustomCtrlApp">
  30. <div ng-controller="ModalDemo">
  31. <h1>Modal example</h1>
  32. <button class="btn btn-default" ng-click="toggleModal()">Open modal</button>
  33. <div>
  34. <modal modal-setting="ModalArgs">
  35. <table>
  36. <tr>
  37. <td>Welcome to Login Form</td>
  38. </tr>
  39. </table>
  40. <p>hello</p>
  41. </modal>
  42. </div>
  43. </div>
  44. </body>
  45. </html>
Now to define the modaldemo controller, add another JavaScript file in the script folder named modaldemo.js with the following code:
  1. CustomCtrlApp.controller("ModalDemo", ['$scope', '$http',
  2. function ($scope, $http) {
  3. $scope.ModalArgs = {
  4. title: 'Modal Window',
  5. width: 750,
  6. height: 550,
  7. showCloseButton: true
  8. };
  9. $scope.toggleModal = function () {
  10. $scope.ModalArgs.method.show();
  11. };
  12. }]);
Now, run the project and the output will be as in the following:



Figure 2: Output Window