AngularJS /强制resolve属性能随时运行 [英] AngularJS / Forcing resolve property to run at anytime

查看:96
本文介绍了AngularJS /强制resolve属性能随时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于大部分开发商的角度认识,解决属性允许保护访问某些页。

As most of angular developers know, Resolve property allows to protect access to certain pages.

下面摘录code的一个简单的例子:

Here a brief example of snippet code:

.config(['$routeProvider', 'securityAuthorizationProvider',
                function ($routeProvider, securityAuthorizationProvider) {
                      $routeProvider.when('/test', {
                          templateUrl: '/myCorrespondingView.tpl.html',
                          controller: 'MyCorrespondingCtrl',
                          resolve: securityAuthorizationProvider.requireAuthenticatedUser
                      });
                }])

让我们假设包含称为链接的网页的点击这里访问受保护的页面的(制作 $ location.path(/测试)引擎盖下)。

Let's suppose a page containing a link called "Click here to access to protected page" (making a $location.path("/test") under the hood).

预期是:当我点击它,使解析作为是拒绝和...不要重定向任何其他页面比调用者的页面。

Expectations are: when I click on it, make resolve as being rejected and...do not redirect to any other pages than the caller's page.

这会导致什么都没有(简单的事实,用户无法到达网页就足够了),但网址改为针对性保护的页面。
=> myApp.com/test
(为什么角不只是在一个成功的结果的承诺的情况下,此替换?? :(,但是这是我刚刚问了另一个问题:<一href=\"http://stackoverflow.com/questions/19653666/angularjs-changing-the-url-only-when-corresponding-templates-resolve-property\">AngularJS /时,对应的模板的决心财产解决)只更改URL。

This causes to...nothing (the simple fact that user can't reach page is enough) but the URL changed to the targeted protected page. => myApp.com/test (why Angular doesn't make this replacement only in case of a successful promise result ?? :(, but this is another question that I've just asked: AngularJS / Changing the URL only when corresponding template's resolve property is solved ).

...然后再次单击...

Then...click again...

看来,解析仍然没有处理,再在所有。

It seems that the resolve remains not processed again at all.

则...点击应用程序其它链路(当然没有完全重定向),无论你以更改URL想要的,reclick被保护链路=> 解析将得到很好的再次处理。

Then...click on other link on the application (without full redirect of course), whichever you want in order to change the URL, and reclick on the protected link => Resolve would be well processed again.

是否有检查当前页面的URL和想要的页面URL,并避免重新处理决心的任务,如果两者是相同的角度机制?

Is there an Angular mechanism that checks for the current page URL and the wanted page URL, and avoids to reprocess resolve task if both are the same?

我真的要强制即使已经URL对应目标一页解析的过程。

I really want to force the resolve process even if the URL already corresponds to the targeted page one.

修订的 * 的***
从这篇文章: http://stackoverflow.com/a/12429133/985949

角手表的位置变化(无论它是完成
  通过输入在地址栏中,点击一个链接或设置
  通过$ location.path位置())。 当它检测到这种变化,它
  $广播事件,$ locationChangeSuccess,并开始路由
  流程。

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 方法完成...
将是巨大的,如果人能强迫评估。

It sounds that the check I supposed above is indeed done by $location.path method... Would be great if one can force the evaluation.

推荐答案

角路线响应在URL中的变化,而不是点击。如果位置没有在浏览器中改变角度不会把它捡起来的。

Angular routes respond to changes in the URL, not to clicks. If the location does not change in the browser angular will not pick it up at all.

看来你应该做的是创建一个监听$ locationChangeSuccess并跟踪上次成功路线的服务。然后,对$ routeChangeError另一个侦听器复位位置上失败的最后一个成功的路线。这将滞留在一个不一致的状态,位置不匹配当前的活动路线prevent你。

It seems what you should do is create a service that listens to "$locationChangeSuccess" and keeps track of the last successful route. Then have another listener on $routeChangeError that resets the location to the last succesful route on a failure. This would prevent you from staying in an inconsistent state where the location does not match your current active route.

一旦你这样做,点击链接再次将导致浏览器尝试再次更改位置,从而再次引发你做出决议。

Once you do that, clicking on the link again would cause the browser to attempt to change the location again, thus triggering your resolves again.

这篇关于AngularJS /强制resolve属性能随时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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