- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一个 Angular 新手。我正在尝试编写一个 Angular 服务,它可以在任何页面上检查用户是否已登录,如果没有,则将他们转发到登录页面,将他们的当前路径作为 GET 参数传递。
我快到了,但还不太行得通。我遇到的问题如下:如果用户转到 #/articles/my-articles/
,他们将被转发到 #/login/?next=%2Farticles%2F:模块 %2F
。
换句话说,看起来 Angular 正在传递路由模式,而不是实际的 URL。
这是我的验证码:
auth.run(['$rootScope', '$location', '$user', 'TOKEN_AUTH', 'PROJECT_SETTINGS', function ($rootScope, $location, $user, TOKEN_AUTH, PROJECT_SETTINGS) {
var MODULE_SETTINGS = angular.extend({}, TOKEN_AUTH, PROJECT_SETTINGS.TOKEN_AUTH);
$rootScope.$on('$routeChangeStart', function (e, next, current) {
if (next.$$route && !next.$$route.anonymous && !$user.authenticated) {
var nextParam = next.$$route.originalPath;
$location.url(MODULE_SETTINGS.LOGIN + '?next=' + nextParam);
}
});
}]);
我可以使用 current.params.module
以一种奇怪的方式获得原始路径 - 但这对我没有帮助,因为 routeChangeStart
似乎被触发了几次次并且 current
对象在除最后一次火灾之外的所有情况下都是未定义的。
这是我的路线文件:
articles.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/articles/:module/', {
templateUrl: 'views/articles/article_list.html',
controller: 'ArticlesListCtrl'
})
.when('/articles/:module/:id/', {
templateUrl: 'views/articles/article_detail.html',
controller: 'ArticlesDetailCtrl'
});
}]);
我该如何解决这个问题?
最佳答案
auth.run(['$rootScope', '$location', '$user', 'TOKEN_AUTH', 'PROJECT_SETTINGS', function ($rootScope, $location, $user, TOKEN_AUTH, PROJECT_SETTINGS) {
var MODULE_SETTINGS = angular.extend({}, TOKEN_AUTH, PROJECT_SETTINGS.TOKEN_AUTH);
$rootScope.$on('$routeChangeStart', function (e, next, current) {
if (!$user.authenticated) {
$location.url(MODULE_SETTINGS.LOGIN + '?next=' + $location.path());
$location.replace();
}
});
}]);
如果登录不是 AngularJS View ,您可能必须提供一个otherwise
路由:
(取决于您的 $locationProvider 配置)
$routeProvider.otherwise({
template: 'Redirecting…',
controller : 'Redirect'
});
...
articles.controller('Redirect', ['$location', function($location) {
if (someConditionThatChecksIfUrlIsPartOfApp) {
location.href = $location.path();
return;
} else {
// Show 404
}
}]);
旁注:您不应该阅读以 $$
为前缀的属性,它们是私有(private)的 AngularJS 变量。
另请注意:不要在您自己的代码中使用 $
前缀 ($user
),这些是公共(public)属性,为 AngularJS 保留。
关于javascript - AngularJS:转发到登录页面进行身份验证,保留路由作为 GET 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20242182/
我在正在构建的应用程序中集成了转发系统,但很难弄清楚在同一流中检索帖子和转发的逻辑。 我有一个 post_reblog 数据透视表来存储 post_id 和 user_id - 转发该帖子的用户的 I
好的,由于阅读了数百本手册和说明页但仍然没有得到它,我正处于某个时刻,我觉得自己非常愚蠢。我希望你能帮帮我! 我有一台运行 Ubuntu Server 的服务器。在服务器上,我正在运行 ddclien
我在我的项目中使用嵌入 jetty 并提出了一些问题。我有这两页: íindex.jsp 结果.jsp 这两个 servlet: 上传 搜索 在 íindex.jsp 中有一个用于上传文件的表单,上传
我正在使用 c# asp.net 创建一个基于 Web 的电子邮件客户端。 令人困惑的是,各种电子邮件客户端在回复电子邮件时似乎以多种不同方式添加原文。 我想知道的是,是否有某种标准化的方式来消除这个
我正在开发一个评估系统,让考生尝试参加考试。因此,呈现问题及其选择的页面是带有 iframe 的页面,在该页面中呈现问题。包含 iframe 的页面包含 JavaScript 计时器。 因此,当问题在
QOTD(每日问题)的 Twilio 新手。我认为这应该是非常基本的,但我似乎找不到答案。 我成功地将一个电话号码转发到我的手机上……很简单……但问题是我经营着几家公司,我想将号码转发到我的手机上。问
我正在尝试在 pod 上公开一个端口 8080,这样我就可以直接从服务器获取 wget。使用端口转发一切正常(kubectl --namespace jenkins port-forward pods
我想转发一个 url,这样如果你在地址栏中输入 www.example.com,你就会被转发到 www.test.com/test.php。 我所做的是在我的区域文件中添加了一个 cname 记录。
我正在尝试在构建 docker 镜像时克隆一个私有(private) github 存储库。我安装了 docker 18.09.2 并根据 Build secrets and SSH forwardi
Grails 2.2.0 我正在探索grails和ajax,也许我是一个狂热的ajax适配器,但是我确实认为它是一个改变游戏规则的人,所以我要走在前面。 数据模型是主要细节(1:n)。客户端中的一个表
Tumblr API似乎不支持帖子的某些细节:评论、转发或点赞的数量。 真的没有办法从每个帖子的 Tumblr API 获取这个吗? 最佳答案 tumblr API 在报告笔记的方式上非常有限。它可以
我正在制作一个greasemonkey脚本,我想要一个链接来前进并修改当前的html并允许用户单击返回以转到原始页面。 我该怎么做?使用jquery+greasemonkey+javascript。主
我的包含文件有一个小问题,我已经对我的问题做了一个简化的模型。假设我正在编译一些需要名为的头文件的源代码 header.h 里面有: #ifndef HEADER_INCLUDED #define H
我遇到了一个奇怪的问题。我有一个自定义组件来为我处理 UICollectionView 的布局。该代码是用 Swift 编写的。 class MyCustomCollectionViewHandler
有什么方法可以导出命令输出的颜色吗? 让我们用一个小例子来解释它: ls -alh --color=auto 将打印目录的彩色内容,而 ls -alh --color=auto | cat 不会打印一
我希望能够转发 url,例如 http://external_url.com/auth => http://internal_url.com:8080/app/auth https://externa
我有一个在 nginx 中混合运行 PHP 和 Tomcat 的域。这是它的样子。我在这个站点上有一个域 example.com 我安装了 Wordpress。这完全没有问题。现在我想要的是,当您导航
我在一种模板化的层次结构中有一堆相关的指标,看起来像 template struct index{ index w; int x, y; }; template <> struct
我之前发布了有关使用Processing与Leap Motion的文章https://www.leapmotion.com/为了构建一个可以检测手颤的应用程序。我相信我需要执行 FFT 才能实现此目的
Peer 必须能够转发数据,以便在点对点覆盖(例如 Chord)中进行广播。当每个节点(对等体)接收到数据时,它会将数据转发到其路由表中的所有其他节点,然后这些节点将再次转发相同的数据,直到环中的所有
我是一名优秀的程序员,十分优秀!