javascript 角度路由 - 重定向到外部站点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18131099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular routes - redirecting to an external site?
提问by jclancy
In an AngularJS routes file there is the option for an otherwise
route, replacing a 404:
在 AngularJS 路由文件中有路由选项otherwise
,替换 404:
$routeProvider
.when(...)
.otherwise({
redirectTo: 'my/path'
});
Is there a way to do this such that the otherwise redirects to a page that is not in the app? I tried
有没有办法做到这一点,否则重定向到不在应用程序中的页面?我试过
$routeProvider
.when(...)
.otherwise({
redirectTo: 'http://example.com'
});
but this jsut tried to redirect to that path in my app, which doesn't exist. The solution I know of is to do manual redirection in a $scope.$on('$routeChangeStart')
in a top-level controller, but this is a lot of code duplication (and it's ugly). Is there a better way?
但是这个 jsut 试图重定向到我的应用程序中不存在的那个路径。我知道的解决方案是$scope.$on('$routeChangeStart')
在顶级控制器中的 a中进行手动重定向,但这是很多代码重复(而且很难看)。有没有更好的办法?
采纳答案by Florian
To my knowledge, this is not possible, as the routeProvider
handles internal routes only.
据我所知,这是不可能的,因为routeProvider
仅处理内部路由。
What you can do though is
你可以做的是
$routeProvider
.when(...)
.otherwise({
controller: "404Controller",
template: "<div></div>"
});
and then just use window.location.href = 'http://yourExternalSite.com/404.html'
in the controller.
然后window.location.href = 'http://yourExternalSite.com/404.html'
在控制器中使用。
回答by Eric
In my case, this worked for me:
就我而言,这对我有用:
$routeProvider
.when('/my-path', {
...typical settings...
}).
.otherwise({
redirectTo: function(obj, requestedPath) {
window.location.href = appConfig.url404;
}
});
回答by Ebrahim
Just take a look at angular.js link behaviour - disable deep linking for specific URLs
看看angular.js 链接行为 - 禁用特定 URL 的深层链接
and use this
并使用这个
target="_self"
目标=“_自我”
<a href="link" target="_self" >link</a>
回答by jltwoo
I won't recommend just using window.location.href in new controller as pointed in other answer because the ngRoute would have set the history to be pointing at the non-existent page (so when user clicks back it will keep redirecting to the 404 page). I tried and it failed.
我不会推荐只在新控制器中使用 window.location.href 就像其他答案中所指出的那样,因为 ngRoute 会将历史设置为指向不存在的页面(因此当用户点击返回时,它将继续重定向到 404页)。我试过了,但失败了。
I would like to point you to my related solution to another SO question here: https://stackoverflow.com/a/27938693/1863794
我想向您指出我对另一个 SO 问题的相关解决方案:https: //stackoverflow.com/a/27938693/1863794
I adopted that and applied to your scenario. I don't think it's that bad of an idea to use MainCtrl
outside of the ng-view since it'll be only declared once unless you have nested layers of ng-view... I don't see any code duplication either, you can place MainCtrl in a separate module if it bothers you so much:
我采用了它并应用于您的场景。我不认为MainCtrl
在 ng-view 之外使用它是一个坏主意,因为它只会被声明一次,除非你有嵌套的 ng-view 层......我也没有看到任何代码重复,你如果 MainCtrl 很困扰您,可以将 MainCtrl 放在单独的模块中:
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when(..)
.otherwise({redirectTo: 'http://yourExternalSite.com/404.html'});
}])
.controller('MainCtrl',[ // <- Use this controller outside of the ng-view!
'$rootScope','$window',
function($rootScope,$window){
$rootScope.$on("$routeChangeStart", function (event, next, current) {
// next.$$route <-not set when routed through 'otherwise' since none $route were matched
if (next && !next.$$route) {
event.preventDefault(); // Stops the ngRoute to proceed with all the history state logic
// We have to do it async so that the route callback
// can be cleanly completed first, so $timeout works too
$rootScope.$evalAsync(function() {
// next.redirectTo would equal be 'http://yourExternalSite.com/404.html'
$window.location.href = next.redirectTo;
});
}
});
}
]);
Cheers
干杯