Problem Statement :
I have a button "add comment" , I should be able to add comment only when i have logged into the system.
But problem is I cannot come back to "add comment" page after login, because I dont know the previous state or cannot get the previous state.
Is there any cleaner solution to this ? Should i have login page a modal rather than a new page ?
I have seen all the questions that is taking about previous state and also the possible answers ( yes, i am taking about $rootscope and $stateChangeSuccess). But it does not clearly put forward the solutions.
Other possible solution is as below http://christopherthielen.github.io/ui-router-extras/example/previous/index.html#
I also saw https://github.com/angular-ui/ui-router/issues/92. but again, i was not sure what is the correct answer.
Could someone clearly state a good solution. Is using rootscope a good one ?
Should i have login page a modal rather than a new page? That is a design decision. You can have whatever fits your app and is to your liking.
Is using rootscope a good one? I feel its quite a good solution. Here's how I would do it:
Save previous state manually:
Create a state change listener:
$rootScope.$on('$stateChangeSuccess', function(event, to, toParams, from, fromParams) {
//save the previous state in a rootScope variable so that it's accessible from everywhere
$rootScope.previousState = from;
});
}]);
Instead of using ui-sref, put a click function for the comment button.
$scope.commentHandler = function(){
//check if user is logged in, let the user post the comment
//else take him to login page
}
Then you can use $state.go($rootScope.previousState.name)
after user has logged in to go back to comment page.
-OR -
You could provide a small login box in place of the "Post comment" button if user is not logged in.