1

I am trying to redirect a WebPage into another .HTML page on button click using $location.path('/Views/Home.html'); but its not happening . The URL is getting changed into the browser but the page is not redirecting..Here is my AngularJS code..

app.controller('LoginformController', function ($scope,$location) {

$scope.LoginCheck = function () {

alert("Trying to login !");

$location.path('/Views/Home.html');

}

scope.PasswordRecovery = function () {
alert("Clicked 2");
}
});

I am getting both the alert functions but not getting the page redirection.Please help..

Lara
  • 2,821
  • 7
  • 39
  • 72

2 Answers2

2

If you are working with AngularJS then your above approach is wrong, you need to do it following way,

First create a routes for "Home"

myApp.config(['$routeProvider', function($routeProvider) {

  $routeProvider.when('/home', {
    templateUrl: 'your respective path/view/home.html',
    controller: 'homeCtrl'
  });

}]); 

Then use $location.path as follow,

$location.path("/home");

Tushar
  • 616
  • 3
  • 9
  • Thanks @Tushar for correcting me ..One doubt where can i add `$location.path("/home");` into the above code snippet provided by you..Thanks – Lara Sep 11 '15 at 09:25
  • you can add "$location.path("/home");" from where you want to redirect your page or after any respective condition, please go ahead and accept it, if it is useful for you – Tushar Sep 11 '15 at 09:28
  • Tushar Please try to sort out my confusion. I am trying to know that where can i add `$location.path("/home")` in the above cod..Please let me know this ..I will accept your answer once it will sort out my problem.Thanks for redirecting me to correct way.. – Lara Sep 11 '15 at 09:31
  • in your above code right now you have put it on correct place, if your going add if else statement for loging conditions, then put in respective condition to redirect on home page after successful login – Tushar Sep 11 '15 at 09:37
  • Could you please create a fiddle for me ..I am very new and not able to understand it completely .I know its use of routing in AngularJs but how to implement it is the issue for me ..Thanks.. – Lara Sep 11 '15 at 09:41
  • its not possible to set whole code on fiddle, you should read about routing – Tushar Sep 11 '15 at 09:57
  • How you access login route, same way you can add route for home page. – Tushar Sep 11 '15 at 09:59
0

Correct me if I'm wrong but if you set the location object with a string, then it will set the default property href to that string. That's why you can set the location directly. What if object default property is changed in the future then

window.location = "http://example.com" 

will break.

Try this instead to be safe:

window.location.href = "http://example.com"
FrankCamara
  • 348
  • 4
  • 9