moff / angular2-flash-messages

Angular 2 flash messages module
52 stars 21 forks source link

angular 2 flash messages show multiple times #41

Open ds-manish opened 6 years ago

ds-manish commented 6 years ago

Hi, the flash messages show multiple times, is there any way to display the latest flash message only. The flash message gets piled up.

2018-05-18 03 37 46 pm

SanchitSahu commented 6 years ago

Did you find a solution for this? I am having the same issue

moff commented 6 years ago

@SanchitSahu, @ds-manish Do you call .show() method multiple times? By default, flash message is visible for 2.5 seconds.

ghost commented 6 years ago

@ds-manish I actually had to look in the source code to refactor an array called messages in the flash-messages.component.js file. What's happening with that array is that it simply pushes each error message that is received, but they don't go away, because of the window timeout function that removes them after 2.5seconds as @moff already mentioned. So what I did there is that I checked if the "options" array hasOwnProperty('timeout'). Whether or not it has that property is determined by the .show(msg, options) methods options object. That means you can either set a timeout or not. So basically window.setTimeout function is set only if you passed a timeout property to the options object. Okay, so this gives you more control over the defaults object. Moving on. Next up is the messages array. What you should do there is simply assign the messages array to an empty array, then you push the new message. And now only the latest message will be shown. This worked for me, hope it works for you too.