如何使用ui-router使用参数获取先前状态 [英] How to get the Previous State with Params using ui-router

查看:110
本文介绍了如何使用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 below http://christopherthielen.github.io/ui-router-extras/example/previous/index.html#

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

I also saw https://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:

手动保存以前的状态:

创建状态更改侦听器:

    $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使用参数获取先前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆