如何重定向到外部URL

时间:2015-03-18 18:42:41

标签: angularjs

尝试使用$window.location.href重定向到外部网页时,该页面只是刷新而不是重定向到预期的网址。

2 个答案:

答案 0 :(得分:28)

<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>

这适合我。

答案 1 :(得分:0)

pdmApp.controller('projectionDetailController', [ '$log', 'projectionDetailService', '$filter', '$window', '$location', '$scope', function ($log, pdService, $filter,$window,$location, $scope) { 

$scope.backToPreviousPage = function () { 
  alert("function is working"); //Add this line to your code to confirm your function is called.
$window.location.href = "http://localhost:port/../sample2.aspx";

} 

} //What is this bracket for?


}]); //you are missing this square bracket.

更改为

pdmApp.controller('projectionDetailController', ['$log', 'projectionDetailService', '$filter', '$window', '$location', '$scope', function($log, pdService, $filter, $window, $location, $scope) {
  $scope.backToPreviousPage = function() {
    alert("function is working"); //Add this line to your code to confirm your function is called.
    $window.location.href = "http://localhost:port/../sample2.aspx";
  };
]);