如何从JavaScript中将用户正确地重定向到Angular中的路由?
我所做的大部分重定向都只是点击一个链接,这很有用。
<a href="#main">Main</a>
但是,如果我这样做:
<button class="btn btn-default" ng-click="performLogin()">Login</button>
$scope.performExternalLogin = function () {
DoSomeAuthenticationStuff();
window.location.href = "/#main";
}
这是第一次使用,但是当用户第二次执行时,我从Angular获得了一个例外。 我怀疑有一种更好的方法可以重定向到一条路线,但我的谷歌搜索技能还没有通过。 我很感激任何帮助。
以下是例外情况: 第112行第381行http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js
中未处理的异常0x800a139e - JavaScript运行时错误:[$ rootScope:infdig] http://errors.angularjs.org/1.2.18/ $ rootScope / infdig?p0 = 10&amp; p1 =%5B%5B%22fn
答案 0 :(得分:4)
在Angular中,您应该使用 $location
服务与窗口的位置进行互动:
$scope.performExternalLogin = function () {
DoSomeAuthenticationStuff();
$location.path('/main'); // Will redirect to `#/main`
}