加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 清除单个toastr消息

发布时间:2020-12-17 17:15:14 所属栏目:安全 来源:网络整理
导读:我想在一次显示的多个toastr消息中清除/隐藏单个toastr消息.是否有任何解决方法,而不是同时清除整个/多个toastr消息.我尝试了以下代码,但对我没用. toastr.clear([toast]); 编号:https://github.com/Foxandxss/angular-toastr 解决方法 你只能清除活动的toa
我想在一次显示的多个toastr消息中清除/隐藏单个toastr消息.是否有任何解决方法,而不是同时清除整个/多个toastr消息.我尝试了以下代码,但对我没用.

toastr.clear([toast]);

编号:https://github.com/Foxandxss/angular-toastr

解决方法

你只能清除活动的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()示例不是最佳实践,因为它会导致内存泄漏.所有toast都存储在openedToasts数组中.如果你打开10个toast,数组大小将是10.过了一会儿,打开的toasts将会消失,但是数组永远不会被清除.

因此,如果以这种方式实现toastr,则必须小心数组.如果要清除阵列中的项目,请确保该项目处于活动状态.

我们如何清除阵列?

要清除数组,我们可以为每个toast注册一个destroy事件:

$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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读