2015-10-14 166 views
4

我想清除/隱藏一次顯示的多個toastr消息中的單個toastr消息。是否有任何解決方法,而不是同時清除整個/多個toastr消息。我嘗試了下面的代碼,但不適合我。清除單個toastr消息

toastr.clear([toast]); 

編號:https://github.com/Foxandxss/angular-toastr

+0

你可以關閉按鈕。檢查配置。 –

+1

我想以編程方式實現它 –

回答

3

只能清除激活toastr不是已駁回toastr

例如:

var openedToast = null; 

$scope.openToast = function(){  
    openedToast = toastr['success']('message 2', 'Title 2'); 
    toastr['success']('this will be automatically dismissed', 'Another Toast');  
} 
//if you click clearToast quickly, it will be cleared. 
$scope.clearToast = function(){ 
    if(openedToast) 
    toastr.clear(openedToast); 
    openedToast = null; //you have to clear your local variable where you stored your toast otherwise this will be a memory leak! 
} 

您可以檢查Demo

注 -toastr demo page顯示的toastr.clear()例子是不是最好的做法,因爲這會導致內存泄漏。所有面包都存儲在openedToasts陣列中。如果您打開10個吐司,數組大小將爲10.一段時間後,打開的吐司將消失,但該數組永遠不會被清除。

因此,如果你以這種方式實施你的toastr,你必須照顧你的數組。如果要清除數組中的項目,請確保該項目處於活動狀態。

如何清除數組?

要清除陣列,我們可以註冊爲每個敬酒毀事件:

$scope.openedToasts = [];  
    $scope.openToast = function() { 
    var toast = toastr['success']('message 1', 'Title 1'); 
    $scope.openedToasts.push(toast); 

    registerDestroy(toast); //we can register destroy to clear the array 
    } 

    function registerDestroy(toast) { 
    toast.scope.$on('$destroy', function(item) { 
     $scope.openedToasts = $scope.openedToasts.filter(function(item) { 
     return item.toastId !== toast.toastId; 
     }); 
    }); 
    } 

在HTML中,您可以檢查長度:

<span>array length: {{openedToasts.length}} </span> 
+0

如果在toastr中還存在關閉按鈕,我們將如何清除該數組或刪除數組索引? –