JanStevens/angular-growl-2

Removing messsage from Controller

Opened this issue · 4 comments

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.

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.

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.

The snippet posted by @fabiosussetto in #102 (comment) will break when uglifying. Here is a minification-safe version:

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

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