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
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.
For ui-router v1.0.0+, state change events have been deprecated.
An updated version of Arjun's state change listener would be
$transitions.onStart({}, function (transition) {
$rootScope.previousState = transition.from();
});