【问题标题】:External pages and Angular JS routing外部页面和 Angular JS 路由
【发布时间】:2015-01-03 01:16:43
【问题描述】:

我在mysite.com/somedir/script.php 有一个 PHP 页面。

当我的 Angular 视图中有指向此脚本的 <a> 标记时,当我单击它们时,Angular 的路由总是将我带回到 mysite.com/index.html

这是我的$routeProvider 配置:

siteModule.config(function($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl:'/views/blog.html',
            controller:'blogController'
        })
        .when('/blog',{
            templateUrl:'/views/blog.html',
            controller:'blogController'
        })
        .when('/work',{
            templateUrl:'/views/work.html',
            controller:'workController'
        });
    // For Pretty URLs
    $locationProvider.html5Mode(true);
});

我还使用一些 Apache mod_rewrite 来处理 .htacess 文件中的 404/页面刷新,我认为这可能会造成干扰:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

前面提到的 PHP 页面是一个有效的(现有的)资源,所以我不确定为什么 Angular 或 Apache 会重定向/重写它。我在<a> 标签上使用target="_blank",并在索引页的<head> 标签中也设置了<base href="/">

我只想能够正常访问我的脚本,而不是被指向索引页面。

【问题讨论】:

  • 请发布一个 a 锚的示例,其中的链接无效。

标签: angularjs apache mod-rewrite


【解决方案1】:

在 javascript 中,您仍然可以使用 window.location.href 转义路由器,因此在路由器的“否则”部分,使用 window.location.href = {路由} 而不是允许它使用默认索引。

查看此问题的其他答案:

Angular routes - redirecting to an external site?

Using $routeProvider to redirect to routes outside of Angular

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-05
    • 2020-11-21
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多