且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何重定向到一个外部URL

更新时间:2022-12-09 14:05:45

 < HTML NG-应用=对myApp>
< HEAD>
< /头>
<机身NG控制器=myCtrl>
    &所述p为H.; < A HREF =htt​​ps://www.google.co.in/><按钮和gt;点击我从模板&LT重定向,/按钮>< / A>< / P>
    < p NG点击=myFunction的()> <按钮和gt;点击我从控制器和LT重定向,/按钮>< / P>
&所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js>&下; /脚本>
<脚本>
    VAR应用= angular.module('对myApp',[]);
    app.controller('myCtrl',函数($窗口,$范围){
        $ scope.myFunction =功能(){
        $ window.location.href ='http://www.google.com'; //你应该在这里有HTTP。
        }
    });
< / SCRIPT>
< /身体GT;
< / HTML>

这为我工作。

When trying to redirect to an external page using $window.location.href, the page is just refreshing and not redirecting to the expected URL.

<html ng-app="myApp">
<head>
</head>
<body ng-controller="myCtrl">
    <p> <a href="https://www.google.co.in/"><button>Click me to redirect from template</button></a></p>
    <p ng-click="myFunction()"> <button>Click me to redirect from controller</button></p>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
    var app = angular.module('myApp',[]);
    app.controller('myCtrl',function($window,$scope){
        $scope.myFunction = function(){
        $window.location.href = 'http://www.google.com'; //You should have http here.
        }
    });
</script>
</body>
</html>

This works for me.