【问题标题】:AngularJS $routeProvider otherwise catches my external redirectionsAngularJS $routeProvider 否则会捕获我的外部重定向
【发布时间】:2014-09-24 10:33:38
【问题描述】:

我在 AngularJS 中使用 $routeProvider 并配置了许多路由。 “否则”部分 已配置,因此它将所有未知路由重定向到我的应用程序的主页。

我想要的只是将浏览器从我的控制器重定向到外部 URL,但我不能。 我尝试使用 $window.location = external、window.location.href = external,但它们不起作用。路由 API 每次都会将我重定向到设置的默认页面。

我如何在有/或没有重大黑客攻击的情况下实现这一目标。不幸的是,它必须通过我的控制器中的 JavaScript 代码来完成。

提前致谢

【问题讨论】:

    标签: angularjs redirect routing external


    【解决方案1】:

    这可能无法完全回答您的问题,但要在路线范围之外导航,您可以使用它

    Links that contain target element
        Example: <a href="/ext/link?a=b" target="_self">link</a>
    
    Absolute links that go to a different domain
        Example: <a href="http://angularjs.org/">link</a> //this one might actually be of use for you
    
    Links starting with '/' that lead to a different base path when base is defined
        Example: <a href="/not-my-base/link">link</a>
    

    【讨论】:

    • 谢谢,这行得通 - 我知道,但我必须从控制器中的 JavaScript 代码进行重定向。
    【解决方案2】:

    AngularJS的路由是用于页面内的路由。

    您应该为此使用本地 location.href。

    编辑:你能给我一个 plunker 吗?你有没有尝试过这条路线?

    $scope.path="http://www.google.com"; $scope.redirect = function(path){ location.href = path; };

    <button ng-click="redirect(path)">Go there</button>
    

    【讨论】:

    • 我不能。它没有效果,也将我重定向到 $routeProvider 的 else 部分中定义的主页。
    猜你喜欢
    • 2016-07-05
    • 2014-02-22
    • 2017-07-03
    • 1970-01-01
    • 2013-10-19
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多