Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to new Page in AngularJS using $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 code

var app = angular.module('location', []);    app.controller('MainCtrl', function($scope,$routeParams, $location) {    $scope.name = 'World';    $scope.go = function() {    $location.absUrl() = 'http://www.google.com';    } }); 
like image 826
Kiran Kumar Avatar asked Sep 18 '13 14:09

Kiran Kumar


People also ask

How to redirect to Another HTML page in AngularJS?

Use the $location Service in AngularJS to Redirect to Another Page. The $location service is used to get the URL of the current page and change it. It can also set up a route and redirect to a new page.

How to redirect to Another page in AngularJS on button click?

You can use $location. path('/newPageUrl') to navigate to new page.

How to change URL path in AngularJS?

To change path URL with AngularJS, $location. path() is passed the new URL as a parameter, and the page is automatically reloaded. This means that if you ever make a change to the URL in an Angular application, then the partial and controller will be reloaded automatically.

What is $location in AngularJS?

Overview. The $location service parses the URL in the browser address bar (based on the window. location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.


1 Answers

$location won't help you with external URLs, use the $window service instead:

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

Note that you could use the window object, but it is bad practice since $window is easily mockable whereas window is not.

like image 58
Fernando Neira Avatar answered Sep 25 '22 12:09

Fernando Neira