使用 $location 重定向到 AngularJS 中的新页面 [英] Redirect to new Page in AngularJS using $location

查看:31
本文介绍了使用 $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.

这篇关于使用 $location 重定向到 AngularJS 中的新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆