madh
madh

Reputation: 163

How to redirect to an external URL

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

Upvotes: 16

Views: 51120

Answers (2)

Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6883

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.

Change To

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";
  };
]);

Upvotes: 0

Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6883

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

Upvotes: 30

Related Questions