2013-10-31 24 views
6

Tôi đã triển khai chỉ thị phương thức và đặt phông nền tùy chọn $modal.open thành false. Tuy nhiên, bây giờ tôi có thể kích hoạt nhiều modals để mở. Có cách nào để ngăn nút kích hoạt kích hoạt khi một phương thức được mở không?Phương thức khởi động giao diện người dùng góc - cách ngăn chặn nhiều phương thức mở

var accountSummaryCtrl = function ($scope, $modal, $log) { 

    $scope.open = function() { 

     var modalInstance = $modal.open({ 
      templateUrl: '/Apps/generic/modal/Templates/AccountSummary.html', 
      controller: ModalInstanceCtrl, 
      backdrop: false 
     }); 

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

Cảm ơn

+2

vô hiệu hóa nút khi bạn mở phương thức –

Trả lời

12

Sử dụng một lá cờ boolean để tránh nó:

var accountSummaryCtrl = function ($scope, $modal, $log) { 

    var opened = false; 

    $scope.open = function() { 

     if (opened) return; 

     var modalInstance = $modal.open({ 
      templateUrl: '/Apps/generic/modal/Templates/AccountSummary.html', 
      controller: ModalInstanceCtrl, 
      backdrop: false 
     }); 

     opened = true; 

     modalInstance.result.then(function (selectedItem) { 
      $scope.selected = selectedItem; 
      opened = false; 
     }, function() { 
      $log.info('Modal dismissed at: ' + new Date()); 
      opened = false; 
     }); 
    }; 
}; 
+0

Bạn có thể giải thích cách hoạt động này không? Tôi thấy '' 'var opened = false;' '' theo sau bởi '' 'if (mở) return;' '' Tôi không thấy cách này giải quyết bất cứ điều gì, ngay cả khi bạn có 2 upvotes trên nó. Liệu '' 'var opened = false''' thuộc về' '' $ scope.open''' function – boatcoder

+1

Bạn nói đúng. 'var opened = false;' là sai địa điểm. Tôi vừa sửa nó. Cảm ơn bạn. –

+0

Cảm ơn người đàn ông, nó đã hoạt động – maverickosama92

0

Tôi đã thực hiện một chỉ thị đơn giản trong đó sử dụng kỹ thuật tương tự như trong câu trả lời J. Bruni của.

/** 
* Directive which helps to prevent multiple modals opened when clicking same button many times. 
* 
* Just replace "ng-click" with "open-single-modal" in your html. Example: 
* 
* <button open-single-modal="openModal()">Open Window</button> 
* 
* NOTICE! "openModal()" function must return modalInstance which is a result of "$uibModal.open()" 
*/ 
app.directive('openSingleModal', function() { 
    return { 
     restrict: 'A', 
     link: function(scope, element, attrs) { 
      var opened = false; 

      element.bind('click', function() { 
       if(opened) { 
        return; 
       } 

       opened = true; 
       var modalInstance = scope.$eval(attrs.openSingleModal); 

       if(!modalInstance || !modalInstance.result || typeof modalInstance.result.then !== 'function') { 
        console.error('Incorrect modal instance returned from the open modal function', element, modalInstance); 
        return; 
       } 

       modalInstance.result.then(function() { 
        opened = false; 
       }, function() { 
        opened = false; 
       }); 
      }); 
     } 
    }; 
}); 

Để chuyển mã cho chỉ thị này, chỉ cần thay đổi "ng-click" thành "open-single-modal" và chức năng được kích hoạt bằng ng-click phải trả về phiên bản phương thức.

Ví dụ. Trước:

<a ng-click="openModal()">Open Me!</a>

Sau:

<a open-single-modal="openModal()">Open Me!</a>

$scope.openModal = function() { 
    return $uibModal.open(...); 
}; 
0
câu trả lời

J Bruni đang rất tốt, nhưng thay vì sử dụng một biến bổ sung, sử dụng những gì bạn đã có với modalInstance. Vì bạn khai báo cá thể phương thức cao hơn, bạn cũng có thể linh hoạt sử dụng biến đó ở nơi khác (chẳng hạn như khi bạn có thể muốn loại bỏ hộp thoại).

link: function(scope, element, attrs) { 
     var modalInstance;  /*assign to undefined if you like to be explicit*/ 

     element.bind('click', function() { 
      if(modalInstance) { 
       return; 
      } 

      modalInstance = scope.$eval(attrs.openSingleModal); 

      if(!modalInstance || !modalInstance.result || typeof modalInstance.result.then !== 'function') { 
       console.error('Incorrect modal instance returned from the open modal function', element, modalInstance); 
       return; 
      } 

      modalInstance.result.then(function() { 
       modalInstance = undefined; 
      }, function() { 
       modalInstance = undefined; 
      }); 
     }); 
    } 
Các vấn đề liên quan