我正在测试以下AngularJS $ location。我不是这个问题。只想检查重定向是否有效:
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服务:
$window.location.href = 'http://www.google.com';
请注意,您可以使用window对象,但这是不好的做法,因为$window可以轻松模拟,而window不能。