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

后端 未结 2 1227
生来不讨喜
生来不讨喜 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.

    0 讨论(0)
  • 2021-02-04 19:14

    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();
    });
    
    0 讨论(0)
提交回复
热议问题