2015-10-14 88 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中还存在关闭按钮,我们将如何清除该数组或删除数组索引? –