角度路由 - 重定向到外部站点?

jcl*_*ncy 9 javascript url-routing angularjs

在AngularJS路由文件中,有一个otherwise路由选项,替换404:

$routeProvider
.when(...)
.otherwise({
    redirectTo: 'my/path'
});
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,否则重定向到不在应用程序中的页面?我试过了

$routeProvider
.when(...)
.otherwise({
    redirectTo: 'http://example.com'
});
Run Code Online (Sandbox Code Playgroud)

但是这个jsut试图重定向到我的应用程序中的那条路径,这条路径不存在.我所知道的解决方案是$scope.$on('$routeChangeStart')在顶级控制器中进行手动重定向,但这是很多代码重复(而且很难看).有没有更好的办法?

Flo*_*ian 6

据我所知,这是不可能的,因为routeProvider只有句柄内部路由.

你可以做的是

$routeProvider
.when(...)
.otherwise({
    controller: "404Controller",
    template: "<div></div>"
});
Run Code Online (Sandbox Code Playgroud)

然后只需window.location.href = 'http://yourExternalSite.com/404.html'在控制器中使用.


Eri*_*ric 5

就我而言,这对我有用:

$routeProvider
.when('/my-path', {
    ...typical settings...
}).
.otherwise({
        redirectTo: function(obj, requestedPath) {
            window.location.href = appConfig.url404;
        }
});
Run Code Online (Sandbox Code Playgroud)


Ebr*_*him 5

看看angular.js 链接行为 - 禁用特定 URL 的深层链接

并使用这个

目标=“_自我”

<a href="link" target="_self" >link</a>
Run Code Online (Sandbox Code Playgroud)