lundi 20 juillet 2015

AngularJS create a sign-out message for the next state only

Here's the deal:
I am trying with to create a global alert message to notify a user he's done an action (for example, logged-out).

I am using SB Admin 2.0 from StartAngular.com.
This is how it looks when I make a proper action.

Global message

Now, the question is actually, how would you do, that this message will last only state?
If I go let's us to '/login' state from here where I took the picture from (dashboard), the message will disappear.

I add a JSFiddle link.
Now it's not supposed to actually show the correct result, rather than give you the code I have currently.

The core is about me using a factory to hold some data I want to save between different states.
After I fill with the factory the data I want to insert in the message, I give the signal with a boolean.

Then, at the beginning of the controller* I seek that signal, and if it is on, I use state change events to remove the log-out signal in the next state**.

$scope.userUtil = userUtil;

if ('loggedOut' in $scope.userUtil){
    $rootScope.$on('$stateChangeSuccess',
        function(event, toState, toParams, fromState, fromParams){
            console.log(1);
            delete $scope.userUtil.loggedOut;
        })
}

Do you think I am going in the right direction? Is there any way you see I can do it in a more simple way?

I appreciate your opinion.

*The controller is bound an abstract state called main (which includes the header, and sidebar)

**The method I am using currently is not perfect, it seems that the function I bind to the event is not a one timer. So if I log out once, I won't see the message again since every state I enter successfully, the log-out signal will be deleted no matter what.

Aucun commentaire:

Enregistrer un commentaire