How to get the Previous State with Params using ui-router

谁都会走 提交于 2019-12-03 00:37:42

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();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!