获取带有参数的先前状态

获取带有参数的先前状态

本文介绍了如何使用 ui-router 获取带有参数的先前状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明:

我有一个添加评论"按钮,我应该只有在我登录系统后才能添加评论.

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 ?

我已经看到了关于先前状态的所有问题以及可能的答案(是的,我正在考虑 $rootscope 和 $stateChangeSuccess).但并没有明确提出解决方案.

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.

其他可能的解决方案如下http://christopherthielen.github.io/ui-router-extras/example/previous/index.html#

Other possible solution is as belowhttp://christopherthielen.github.io/ui-router-extras/example/previous/index.html#

我也看到了https://github.com/angular-ui/ui-router/issues/92.但同样,我不确定正确的答案是什么.

I also sawhttps://github.com/angular-ui/ui-router/issues/92. but again, i was not sure what is the correct answer.

有人能清楚地说出一个好的解决方案吗?使用 rootscope 好吗?

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.

使用 rootscope 好吗? 我觉得这是一个很好的解决方案.这是我的做法:

Is using rootscope a good one? I feel its quite a good solution. Here's how I would do it:

手动保存之前的状态:

创建一个状态变化监听器:

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;
    });
  }]);

不要使用ui-sref,而是为评论按钮放置一个点击功能.

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
}

然后您可以在用户登录后使用$state.go($rootScope.previousState.name)返回评论页面.

Then you can use $state.go($rootScope.previousState.name) after user has logged in to go back to comment page.

-或-

如果用户未登录,您可以提供一个小的登录框来代替发表评论"按钮.

You could provide a small login box in place of the "Post comment" button if user is not logged in.

这篇关于如何使用 ui-router 获取带有参数的先前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 01:44