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

后端 未结 2 1229
生来不讨喜
生来不讨喜 2021-02-04 18:35

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

2条回答
  •  余生分开走
    2021-02-04 18:53

    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.

提交回复
热议问题