我有一个工作的Angular.js应用程序,启用了HTML5模式.
$location.Html5mode(true).hashbang("!");
Run Code Online (Sandbox Code Playgroud)
我想要实现的是获取一些URL或<a>标签来执行正常的浏览行为,而不是使用HTML5历史记录API更改地址栏中的URL并使用Angular控制器处理它.
我有这个链接:
<a href='/auth/facebook'>Sign in with Facebook</a>
<a href='/auth/twitter'>Sign in with Twitter</a>
<a href='/auth/...'>Sign in with ...</a>
Run Code Online (Sandbox Code Playgroud)
我希望浏览器将用户重定向到,/auth/...以便用户重定向到身份验证服务.
有什么方法可以做到这一点吗?
我在Angular中编写了一个Web应用程序的一部分.为了确保覆盖所有路由,我想添加一个redirectTo属性$routeProvider,以便将无效路由返回到Web应用程序的根目录,而不使用Angular.
我试过了:
$routeProvider.otherwise({
redirectTo: '/'
});
Run Code Online (Sandbox Code Playgroud)
但显然这只是在URL的Angular控制部分中的路由,因此用户将被重定向到一个URL http://app.com/angular-part-of-web-app#,而不是http://app.com我希望他们去的地方.
我通过将一个空白的部分用作'404'页面,然后是一个只使用该$window对象重定向到所需页面的控制器来解决这个问题:
routes.js
// Redirect to site list.
$routeProvider.when('/404', {
templateUrl: '/partials/404.html',
controller: 'RedirectCtrl'
});
// Redirect to the 404 page.
$routeProvider.otherwise({
redirectTo: '/404'
});
Run Code Online (Sandbox Code Playgroud)
controllers.js
// Controller to redirect users to root page of site.
.controller('RedirectCtrl', ['$scope', '$window', function ($scope, $window) {
$window.location.href = '/';
}]);
Run Code Online (Sandbox Code Playgroud)
然而,这是掀起'太hacky,必须是更好的方式'的警钟.在Angular有更好的方法吗?
编辑:Angular路线 - 重定向到外部网站?没有回答同一个问题.我将打开我的问题,而不是将其标记为重复(现在),因为Angular世界移动如此之快,之前的答案可能不再是这样.