How to use $Uibmodal in Angularjs?

$uibModal is a service provided by the UI Bootstrap library for AngularJS (1.x) that helps you create modals (dialogs or pop-ups) in your application. To use $uibModal, follow these steps:

  1. Install and include UI Bootstrap:

First, you need to install the UI Bootstrap library and its dependencies. You can either download it from the official website (https://angular-ui.github.io/bootstrap/) or use a CDN like the following:

HTML code

<!-- AngularJS and UI Bootstrap styles -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-csp.css">

<!-- AngularJS and UI Bootstrap scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
  1. Create an AngularJS module and controller:

Create a new AngularJS module and controller. Add the ui.bootstrap dependency to your module:

JS code

angular.module('app', ['ui.bootstrap'])
  .controller('MainController', function($scope, $uibModal) {
    // Your main controller logic here
  });
  1. Create the modal template:

Create an HTML file (e.g., modalTemplate.html) that will be used as the modal’s content:

html code

<div class="modal-header">
  <h3 class="modal-title">Modal Title</h3>
</div>
<div class="modal-body">
  This is the modal content.
</div>
<div class="modal-footer">
  <button class="btn btn-primary" ng-click="$ctrl.ok()">OK</button>
  <button class="btn btn-warning" ng-click="$ctrl.cancel()">Cancel</button>
</div>
  1. Create the modal controller:

Create a new AngularJS controller that will handle the modal’s logic:

JS Code

angular.module('app')
  .controller('ModalController', function($uibModalInstance) {
    var $ctrl = this;

    $ctrl.ok = function() {
      $uibModalInstance.close('ok');
    };

    $ctrl.cancel = function() {
      $uibModalInstance.dismiss('cancel');
    };
  });
  1. Open the modal:

In your main controller, create a function that opens the modal using $uibModal.open():

JS Code

$scope.openModal = function() {
  var modalInstance = $uibModal.open({
    templateUrl: 'modalTemplate.html',
    controller: 'ModalController',
    controllerAs: '$ctrl'
  });

  modalInstance.result.then(
    function(result) {
      console.log('Modal closed with:', result);
    },
    function(reason) {
      console.log('Modal dismissed with:', reason);
    }
  );
};
  1. Add a button to open the modal:

Finally, add a button to your HTML that calls the openModal() function:

html code

<body ng-controller="MainController">
  <button ng-click="openModal()">Open Modal</button>
</body>

Now, when you click the “Open Modal” button, a modal will appear with the content and behavior defined in modalTemplate.html and ModalController. You can further customize the modal’s appearance, size, and other options by modifying the $uibModal.open() configuration.

Complete example

Here it is:

  1. index.html – Main HTML file:
<!DOCTYPE html>
<html ng-app="app">
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-csp.css">

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
    <script src="app.js"></script>
  </head>
  <body ng-controller="MainController">
    <button ng-click="openModal()">Open Modal</button>
  </body>
</html>
  1. app.js – Main JavaScript file:
angular.module('app', ['ui.bootstrap'])
  .controller('MainController', function($scope, $uibModal) {
    $scope.openModal = function() {
      var modalInstance = $uibModal.open({
        templateUrl: 'modalTemplate.html',
        controller: 'ModalController',
        controllerAs: '$ctrl'
      });

      modalInstance.result.then(
        function(result) {
          console.log('Modal closed with:', result);
        },
        function(reason) {
          console.log('Modal dismissed with:', reason);
        }
      );
    };
  })
  .controller('ModalController', function($uibModalInstance) {
    var $ctrl = this;

    $ctrl.ok = function() {
      $uibModalInstance.close('ok');
    };

    $ctrl.cancel = function() {
      $uibModalInstance.dismiss('cancel');
    };
  });
  1. modalTemplate.html – Modal template:
<div class="modal-header">
  <h3 class="modal-title">Modal Title</h3>
</div>
<div class="modal-body">
  This is the modal content.
</div>
<div class="modal-footer">
  <button class="btn btn-primary" ng-click="$ctrl.ok()">OK</button>
  <button class="btn btn-warning" ng-click="$ctrl.cancel()">Cancel</button>
</div>

This example demonstrates how to create and use a modal using $uibModal in AngularJS with the UI Bootstrap library. When you click the “Open Modal” button, a modal with the content from modalTemplate.html will appear, and you can interact with it using the behavior defined in the ModalController.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts

CSS3 Borders

CSS3, the latest evolution of the Cascading Style Sheets language, has brought a plethora of new features that make designing

Read More