angular-toastr icon indicating copy to clipboard operation
angular-toastr copied to clipboard

Used in a service, toastr.clear() not working

Open OpenNoob opened this issue 10 years ago • 11 comments

Hi,

As in the title, calling the clear method doesn't work when toastr is used in service/factory. Calling them to show works, no problem.

How exactly can this be used in a service?

Thanks

OpenNoob avatar Nov 08 '15 04:11 OpenNoob

It shouldn't give you any problem at all. In Angular, services (like toastr) is a singleton, so no matter where you call it, it will always have the same data.

Foxandxss avatar Nov 08 '15 11:11 Foxandxss

@OpenNoob

Have you found a solution for this issue? I am running in exactly the same problem.

ZuSe avatar Mar 07 '16 17:03 ZuSe

I would love some reproduction.

Foxandxss avatar Mar 07 '16 17:03 Foxandxss

Add an empty array and it wont work. Leave out the array and it works. Add one toast to the array given as input to the service.function and it does not work. I have like

myCtrl.function = function(){ ... let myToast = toast.info(....) FileManager.uploadFile(fileToUpload, myToast);

}

FileManager.uploadFile = function(fileToUpload, mytoast){ Upload.uploadFile(fileToUpload).then( toastr.clear([mytoast]) // does not work }

ZuSe avatar Mar 07 '16 19:03 ZuSe

clear accepts a toast, not an array of toasts. An array of toasts could be a nice addition tho.

Foxandxss avatar Mar 08 '16 19:03 Foxandxss

@Foxandxss your example here makes it look a lot like the toast is supposed to be in an array. I'm also having trouble with this and cannot get it to work, are there specific toastrConfig settings that need to be set in order for clear to work?

haffmaestro avatar Jun 14 '16 19:06 haffmaestro

And you are totally right. That syntax is used when you have an optional parameter, say foo(arg1, [arg2]) but could mean an array as well.

You shouldn't need to do something special to clear toasts. What are you trying?

Foxandxss avatar Jun 14 '16 19:06 Foxandxss

@Foxandxss, thanks for fast response! Basically I have a wrapper of toastr that does the following,

function show(msg, opts = {}) {

    var notification = toastr.info(msg, opts);
    notification.removeSelf = () => toastr.clear(notification);

    return notification;
  }

And when I do notification.removeSelf() nothing happens.

haffmaestro avatar Jun 14 '16 19:06 haffmaestro

removeSelf with clear appears to work as expected in plunkr, I expect it may be something related to the rest of (our) codebase or dependencies:

http://plnkr.co/edit/BLi5KUU0yEJU4wrT6sDv?p=preview

Robinson7D avatar Jun 21 '16 16:06 Robinson7D

In reaction to this comment, I believe that it should be made clearer in the documentation that this is not an array notation, but rather an optional parameter notation. I fell into that trap too although once the "optional parameter" explanation comes in, it feels like a "dumb me, I know that!!!" moment.

Amenel avatar Sep 23 '16 09:09 Amenel

I encountered this also. Seems $destroy doesn't always cause the element to be removed immediately. I think I've fixed this in #219 (at least in my case). Also added support for [toast1, toast2] to accommodate the ambiguous instructions.

ahgittin avatar Jan 24 '17 16:01 ahgittin