angular-toastr
angular-toastr copied to clipboard
Used in a service, toastr.clear() not working
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
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.
@OpenNoob
Have you found a solution for this issue? I am running in exactly the same problem.
I would love some reproduction.
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 }
clear accepts a toast, not an array of toasts. An array of toasts could be a nice addition tho.
@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?
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, 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.
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
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.
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.