如何使用AngularJS隐藏/显示相同的模态实例?

Gui*_*rin 12 angularjs angular-ui-bootstrap angular-strap

我目前正在使用angular-ui-bootstrap $ modal来显示一个对话框,让用户搜索并选择一个文件.文件列表来自box.com,因此我使用框API搜索文件并生成缩略图以显示在搜索结果中的每个文件旁边.

缩略图生成非常慢,用户需要在同一页面中多次调用此搜索对话框.因此,如果搜索对话框在重新打开时包含先前的搜索结果,则对用户有帮助.

问题是我如何重复使用(即显示/隐藏)相同的模态实例?Angular-ui似乎每次都会破坏/重新创建对话框.角带相同的东西.

编辑

这是一个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');
  };

};
Run Code Online (Sandbox Code Playgroud)

use*_*240 0

要创建模式,您可以这样做:

var planModal = $modal({scope: $scope,
            template: 'modalTemplate.html',
            show: false});
Run Code Online (Sandbox Code Playgroud)

“show”属性设置为 false,这会在加载模式时停止显示模式。

要显示此模式,我们可以这样做:

planModal.$promise.then(planModal.show);
Run Code Online (Sandbox Code Playgroud)

同样,要隐藏此模式,我们可以这样做:

planModal.$promise.then(planModal.hide);
Run Code Online (Sandbox Code Playgroud)