Clear one message toastr

I would like to clear / hide one toastr message among several toastr messages displayed at a time. Is there any workaround for this, rather than clearing all / several toastr messages at once. I tried the following code but did not work for me.

toastr.clear([toast]);

Link: https://github.com/Foxandxss/angular-toastr

+4
source share
1 answer

You can clear the active toastr, not already lost toastr .

For example:

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!
}

You can check out the demo

- toastr.clear(), toastr demo, , . openedToasts. 10 , 10. , .

, toastr , . , , .

?

, 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>
+3

Source: https://habr.com/ru/post/1611544/


All Articles