javascript AngularJS - 从代码重定向到另一个域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18352162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:35:52  来源:igfitidea点击:

AngularJS - Redirect to another domain from code

javascriptredirectangularjs

提问by Arnab Das

I've set up one plunker to describe my problem: Plnkr

我已经设置了一个 plunker 来描述我的问题:Plnkr

Actually I want to navigate to another page in another domain (added one handler to a button click). But it's not working.

实际上,我想导航到另一个域中的另一个页面(向按钮单击添加了一个处理程序)。但它不起作用。

Thanks in advance.

提前致谢。

Update

更新

window.location.href = "http://google.com";is working but $location.absUrlservice from AngularJS default is not working.

window.location.href = "http://google.com";正在工作,但$location.absUrl来自 AngularJS 默认的服务不工作。

回答by Alexis N-o

The $locationservice does not allow to reload the page. The Angular way to perform a redirection to another domain is to use the $windowservice:

$location服务不允许重新加载页面。执行重定向到另一个域的 Angular 方法是使用该$window服务

$window.location.href = 'http://google.com'

回答by Xiu

Unfortunately, the $location service allow you only to change the relative path of your web application whether you want to be redirected to another site you have to use window.location.href

不幸的是,$location 服务只允许您更改 Web 应用程序的相对路径,无论您想重定向到另一个站点,您都必须使用 window.location.href

回答by Chandermani

You not not defined ng-appon you html. And also not wired up your controller on html. See updated fiddle here

你没有ng-app在你的html上定义。而且也没有在 html 上连接你的控制器。在此处查看更新的小提琴

http://plnkr.co/edit/pPJcWtLrbQOHDT5mXleK?p=preview

http://plnkr.co/edit/pPJcWtLrbQOHDT5mXleK?p=preview