Angularjs 清除单个toastr消息
我想在一次显示的多条toastr消息中清除/隐藏一条toastr消息。是否有解决方法,而不是同时清除整个/多个toastr消息。我尝试了以下代码,但对我无效Angularjs 清除单个toastr消息,angularjs,toastr,Angularjs,Toastr,我想在一次显示的多条toastr消息中清除/隐藏一条toastr消息。是否有解决方法,而不是同时清除整个/多个toastr消息。我尝试了以下代码,但对我无效 toastr.clear([toast]); Ref:您只能清除活动的toastr,而不是已解除的toastr 例如: var openedToast = null; $scope.openToast = function(){ openedToast = toastr['success']('message 2',
toastr.clear([toast]);
Ref:您只能清除活动的
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!
}
您可以查看
注意-
中显示的toastr.clear()
示例不是最佳做法,因为它会导致内存泄漏。所有烤面包都存储在openedToasts
数组中。如果你打开10个烤面包,数组大小将是10。一段时间后,打开的祝酒器将消失,但阵列将永远不会被清除
因此,如果您以这种方式实现toastr,您必须小心您的数组。如果要从阵列中清除项目,请确保该项目处于活动状态
如何清除阵列?
要清除阵列,我们可以为每个toast注册销毁事件:
$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>
数组长度:{{openedtoats.length}
您可以使用“关闭”按钮。检查配置。我想通过编程实现它。如果toastr中也有关闭按钮,我们如何清除数组或删除数组索引?