正如大多数 Angular 开发人员所知,Resolve属性允许保护对某些页面的访问。
这里是一段代码的简短示例:
.config(['$routeProvider', 'securityAuthorizationProvider',
function ($routeProvider, securityAuthorizationProvider) {
$routeProvider.when('/test', {
templateUrl: '/myCorrespondingView.tpl.html',
controller: 'MyCorrespondingCtrl',
resolve: securityAuthorizationProvider.requireAuthenticatedUser
});
}])
假设一个页面包含一个名为“单击此处访问 protected 页面”的链接(在幕后制作一个$location.path("/test")
).
期望是:当我点击它时,将 resolve
设置为被 rejected
并且...除了调用者的页面之外,不会重定向到任何其他页面。
这导致...什么都没有(用户无法访问页面这一简单事实就足够了)但是 URL 更改为目标 protected 页面。
=> myApp.com/test
(为什么 Angular 仅在 promise 结果成功的情况下才进行此替换??:(,但这是我刚刚提出的另一个问题:AngularJS / Changing the URL only when corresponding template's resolve property is solved)。
然后...再次点击...
似乎 resolve
仍然没有再次处理。
然后...单击应用程序上的其他链接(当然没有完全重定向),以更改 URL 为准,然后重新单击 protected 链接 => Resolve
将是再次处理好。
是否有一种 Angular 机制可以检查当前页面 URL 和所需页面 URL,并在两者相同时避免重新处理 resolve
任务?
我真的很想强制执行 resolve
过程,即使 URL 已经对应于目标页面。
**已更新 ******** 来自这篇文章:https://stackoverflow.com/a/12429133/985949
Angular watches for a location change (whether it’s accomplished through typing in the location bar, clicking a link or setting the location through $location.path()). When it senses this change, it $broadcasts an event, “$locationChangeSuccess,” and begins the routing process.
听起来我上面假设的检查确实是由 $location.path
方法完成的......
如果能强制求值就好了。
最佳答案
Angular 路由响应 URL 的变化,而不是点击。如果位置在浏览器中没有改变,angular 根本不会拾取它。
看来您应该做的是创建一个监听“$locationChangeSuccess”并跟踪上次成功路线的服务。然后在 $routeChangeError 上有另一个监听器,它将位置重置为失败时最后一次成功的路由。这将防止您处于位置与您当前事件路线不匹配的不一致状态。
一旦你这样做,再次点击链接将导致浏览器再次尝试更改位置,从而再次触发你的解决。
关于javascript - AngularJS/强制解析属性随时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19659183/