Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/show same modal instance with AngularJS?

I'm currently using angular-ui-bootstrap $modal to display a dialog which lets the user search for and pick a file. The list of files comes from box.com, so I use the box API to search for files and generate a thumbnail to display beside each file in the search result.

Thumbnail generation is quite slow and the user needs to call this search dialog multiple times in the same page. So it would be helpful for the user if the search dialog would contain the previous search results when re-opened.

The question is how can I re-use (i.e. show/hide) the same modal instance ? Angular-ui seems to destroy/recreate the dialog each time. Same thing with angular-strap.

Edit

Here is a Plunkr:

var app = angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function($scope, $modal, $log) {

  $scope.open = function() {

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,

    });

    modalInstance.result.then(function() {
      $log.info('Modal closed at: ' + new Date());
    }, function() {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
};

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

var ModalInstanceCtrl = function($scope, $modalInstance) {

  $scope.friends = [{
    name: 'John',
    phone: '555-1276'
  }, {
    name: 'Mary',
    phone: '800-BIG-MARY'
  }, {
    name: 'Mike',
    phone: '555-4321'
  }, {
    name: 'Adam',
    phone: '555-5678'
  }, {
    name: 'Julie',
    phone: '555-8765'
  }, {
    name: 'Juliette',
    phone: '555-5678'
  }];

  $scope.ok = function() {
    $modalInstance.close('close');
  };

  $scope.cancel = function() {
    $modalInstance.dismiss('cancel');
  };

};
like image 445
Guillaume Morin Avatar asked Feb 05 '14 13:02

Guillaume Morin


People also ask

How do I hide a modal pop up?

Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.

How do you toggle modals?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

What is modal hide?

modal('hide') Manually hides a modal. Returns to the caller before the modal has actually been hidden (i.e. before the hidden.

What is modal in AngularJS?

From an AngularJS perspective, a modal window is nothing more than a Controller than manages a View-Model, interacts with Services, and is rendered by a View. The same it true for every other UI-oriented component in your AngularJS application.


1 Answers

To create/hide a ng-strap modal you can use it like this:

     var modalInstance;
        $scope.open = function() {    
            modalInstance= $modal.open({
                   templateUrl: 'myModalContent.html',
                   controller: ModalInstanceCtrl,        
            });
 //This is how to show the modal.

        modalInstance.$promise.then(modalInstance.show);
        };

    //When u want to hide the modal use this as written below:    
    $scope.close = function() {
        modalInstance.hide();
    };
like image 158
Rahul Tiwari Avatar answered Oct 01 '22 01:10

Rahul Tiwari