Darren Christopher
Darren Christopher

Reputation: 4821

Angular JS - Modal not shown properly

I am learning to use Modal in AngularJS and Bootstrap 4 framework. I have manage to show it, but it is not shown properly, i.e. blocking components in background and cannot be shown if animation: true. I am not sure what causing this because I have inject ngAnimate and ui.bootstrap in my app controller.

Angular and ui-bootstrap version used

Below I will provide my code.

View.html

<div class="container-fluid content-container" ng-app="listEmployee">
<div class="change-password-modal-container"></div>
  <script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
        <h3 class="modal-title" id="modal-title">I'm a modal!</h3>
    </div>
    <div class="modal-body" id="modal-body">
        <ul>
            <li ng-repeat="item in ctrl.items">
                <a href="#" ng-click="$event.preventDefault(); ctrl.selected.item = item">{{ item }}</a>
            </li>
        </ul>
        Selected: <b>{{ ctrl.selected.item }}</b>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="ctrl.ok()">OK</button>
        <button class="btn btn-warning" type="button" ng-click="ctrl.cancel()">Cancel</button>
    </div>
  </script>
  ... <!--other html elements -->
</div>

Below is my code in controller which is related to showing the modal

Controller.js

var ctrl = this;
    ctrl.items = ['item1', 'item2', 'item3'];
    ctrl.animationsEnabled = false;
    ctrl.open = function (size, parentSelector) {
        var parentElem = parentSelector ? 
          angular.element($document[0].querySelector('.change-password-modal-container ' + parentSelector)) : undefined;
        var modalInstance = $uibModal.open({
          animation: ctrl.animationsEnabled,
          ariaLabelledBy: 'modal-title',
          ariaDescribedBy: 'modal-body',
          templateUrl: 'myModalContent.html',
          controller: 'ModalInstanceCtrl',
          controllerAs: 'ctrl',
          size: size,
          appendTo: parentElem,
          resolve: {
            items: function () {
              return ctrl.items;
            }
          }
        });
        modalInstance.result.then(function (selectedItem) {
          ctrl.selected = selectedItem;
        }, function () {
          $log.info('Modal dismissed at: ' + new Date());
        });
  };

The z-index of my content is 1

mystyle.css

#content {
    z-index: 1;
}

Below is the screenshot when the animation:false

no animation

Below is the screenshot when the animation:true (i.e. modal is not visible, it's like the modal is behind the screen)

Animation true

Any help would be appreciated.

Upvotes: 1

Views: 595

Answers (1)

Darren Christopher
Darren Christopher

Reputation: 4821

After reading this issue in GitHub, turns out this is the problem when using Bootstrap 4. Bootstrap 4 has different class names with Bootstrap 3, in this case, it is .in in version 3 become .show in version 4.

Following as suggested by IdanCo in the thread, which I rewrite below (so it will be easier for others to read) fixed this issue.

Change the class names from ui-bootstrap-tpls-###.js as below.

             'class': 'modal-backdrop',
             'ng-style': '{\'z-index\': 1040 + (index && 1 || 0) + index*10}',
             'uib-modal-animation-class': 'fade',
             'modal-in-class': 'in'   //change this
             'modal-in-class': 'show' //to this
           });
           if (modal.backdropClass) {
             backdropDomEl.addClass(modal.backdropClass);
@@ -477,7 +477,7 @@
           'ng-style': '{\'z-index\': 1050 + $$topModalIndex*10, display: \'block\'}',
           'tabindex': -1,
           'uib-modal-animation-class': 'fade',
           'modal-in-class': 'in'   //change this
           'modal-in-class': 'show' //to this
         }).append(content);
         if (modal.windowClass) {
           angularDomEl.addClass(modal.windowClass);

Hope this update could help someone in the future.

Upvotes: 1

Related Questions