AngularJS /模板对应的决心属性时只更改URL解决 [英] AngularJS / Changing the URL only when corresponding template's resolve property is solved

查看:75
本文介绍了AngularJS /模板对应的决心属性时只更改URL解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决是一个有趣的属性,以prevent要显示的模板对于一些条件逻辑处理一个承诺的结果(解决或拒绝)。

Resolve is an interesting property to prevent a template to be displayed regarding some conditional logic dealing with a promise result (solved or rejected).

我用它在我的应用程序,这里有一个概念上的示例:

I use it in my application, here's a conceptual sample:

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

所以,这code预计将显示 /myCorrespondingView.tpl.html 内容当且仅当 securityAuthorizationProvider.requireAuthenticatedUser 解析(承诺期)。

So, this code expects to display /myCorrespondingView.tpl.html content if and only if securityAuthorizationProvider.requireAuthenticatedUser is resolved (promise term).

我的期望是:我不想更改URL为 http://myApp.com/test 如果这个承诺在解析部分将被拒绝。原因很简单,我不想白白重装previous控制器,如果我拒绝的逻辑是让自己的当前页面上的用户(因此需要重定向到后者)。

My expectation is: I don't want to change the URL to http://myApp.com/test if this promise in resolve part is rejected. The reason is simple, I don't want to uselessly reload the previous controller if my rejection logic is to let the user on his current page (thus needing a redirection to this latter) .

有没有一种有效的方式与决心比在源转移的条件逻辑,这意味着到previous控制器等属性来实现这一目标?

Is there an efficient way to achieve this with the resolve property other than shifting the conditional logic at the source, meaning into the previous controller?

推荐答案

我知道这是一个老问题,但对于谁是寻找一个答案所有用户,我会提供我的解决方案。

I know this is an old question, but for all users who are looking for an answer, i will provide my solution.

我有什么是显示在整个页面的弹出/模式/警报。在这个弹出式是一个历史,所以你可以打开一个新视图,可以回到旧观点。结果
我添加了一个功能,你可以回去与鼠标或浏览器的后退按钮。

I have a popup / modal / alert whatever which is displayed over the whole page. in this popup is a history, so you can open a new view and can go back to the old view.
I added a feature that you can go back with your back-button on mouse or browser.

我的JS:

SYSTEM.run( [ "$rootScope", "Popup", function( $rootScope, Popup ) {
    $rootScope.$on( "$routeChangeStart", function ( e ) {
        if ( $rootScope.popup ) {
            Popup.back();
            e.preventDefault();
            return;
        }
    } );
} );

所以你可以做的是,让接下来的路线(以$ routeChangeStart提供),并检查这个网址被固定并运行服务。如果您的服务已解决重定向到URL和检查变量设置为true / false,所以在接下来的变化就不会再次检查服务。

So what you could do is, get the next route (provided in $routeChangeStart) and check if this url is secured and run your service. if your service is resolved redirect to the url and set the checking variable to true/false, so on the next change you won't check your service again.

希望这有助于。

这篇关于AngularJS /模板对应的决心属性时只更改URL解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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