使用 $location 重定向到 AngularJS 中的新页面
问题描述:
我正在使用以下 AngularJS $location 进行测试.我不知道这有什么问题.只想检查重定向是否有效:
I am testing with the following AngularJS $location. I don't what's the problem with this. Just want to check if the redirection is working or not:
HTML
<body data-ng-controller="MainCtrl">
Hello {{name}}!
<button ng-click='go()'>Go</button>
</body>
AngularJS 代码
var app = angular.module('location', []);
app.controller('MainCtrl', function($scope,$routeParams, $location) {
$scope.name = 'World';
$scope.go = function() {
$location.absUrl() = 'http://www.google.com';
}
});
答
$location 不会帮助您处理外部 URL,请改用 $window 服务:
$location won't help you with external URLs, use the $window service instead:
$window.location.href = 'http://www.google.com';
请注意,您可以使用 window 对象,但这是不好的做法,因为 $window 很容易模拟 而 window 不是.
Note that you could use the window object, but it is bad practice since $window is easily mockable whereas window is not.