JanStevens / angular-growl-2

growl-like notifications for angularJS projects
MIT License
285 stars 97 forks source link

Removing messsage from Controller #102

Open eduardoinnorway opened 8 years ago

eduardoinnorway commented 8 years ago

There are many scenarios where you want to destroy the message/messages from the Controller for example on state change with ui-router, the message is still there. Or destroy the error message ones the form is valid again.

I think you should add to the docs that you can remove messages from the Controller like this for example:


var formErrorMessage = growl.error('You have an error in your form.'); //Instant show just example

var destroyErrorMessage = function() { formErrorMessage.destroy(); };

//Call destroyErrorMessage() after validation success, next state or where ever.

Note! above is just example to show and destroy, so this will open the error message instantly.

ibnIrshad commented 8 years ago

I second this idea. It might be easier to also have growl.clearAll(), which clears all visible notifications. I would want to use this after a state change with ui-router.

fabiosussetto commented 8 years ago

With the latest version (0.7.5), you can do it by using the growlMessages service like this:

.run(function ($rootScope, growlMessages) {
        $rootScope.$on('$stateChangeSuccess', () => {
            growlMessages.destroyAllMessages();
        });
    });

Note that on 0.7.3 there was an issue with the way destroyAllMessages looped over the messages internally, so that wouldn't work.

janpapenbrock commented 8 years ago

The snippet posted by @fabiosussetto in https://github.com/JanStevens/angular-growl-2/issues/102#issuecomment-143724500 will break when uglifying. Here is a minification-safe version:

.run(['$rootScope', 'growlMessages', function ($rootScope, growlMessages) {
        $rootScope.$on('$stateChangeSuccess', function () {
            growlMessages.destroyAllMessages();
        });
    }]);
fabiosussetto commented 8 years ago

@janpapenbrock you're right, I copied from one of our projects where we use ng-annotate to automatically deal with the problem.