$window.location.href NOT WORKING in AngularJS
Asked Answered
B

6

9

I'm building a basic AngularJS Login page and the $window.location.href did not re-direct the page to a new html in my system, hosted by WAMP. I even tried re-directing it to google. Nothing happens. I tried all the available solutions here and nothing seems to work. Any solutions ?

JS followed by HTML

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';


  $scope.submit = function() {
    if ($scope.username && $scope.password) {
      var user = $scope.username;
      var pass = $scope.password;
      if (pass == "admin" && user == "[email protected]") {
        alert("Login Successful");
        $window.location.href = "http://google.com"; //Re-direction to some page
      } else if (user != "[email protected]") {
        alert("Invalid username");
      } else if (pass != "admin" && user == "[email protected]") {
        alert("Invalid password");
      }
    } else {
      alert("Invalid Login");
    }
  }


});
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script src="login.js"></script>
  <link rel="stylesheet" href="login.css">
</head>

<body ng-controller="MainCtrl">
  <form>
    <label>Username:</label>
    <input type="email" ng-model="username" class="lab1" />
    </br>
    </br>
    <label></label>Password:</label>
    <input type="password" ng-model="password" class="lab2">
    </br>
    <button type="submit" ng-click="submit()" class="buttonclass">login</button>
  </form>
</body>

</html>
Bebel answered 1/12, 2015 at 10:19 Comment(4)
Try withouth the $ sign. window.location.href = "[email protected]"Maroon
Use basic Javascript window redirect .. that is without $. window.location.href.Histology
It worked. Thank you :) But why doesn't it work with the dollar sign ?Bebel
@VarunKN check this link.Histology
Z
4

In angular js you can use $location. Inject it in your controller :

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

And if you want to redirect to google use its url() method :

$location.url('http://google.fr');

you can also use path() method for relative url :

$location.path('home'); // will redirect you to 'yourDomain.xx/home'

https://docs.angularjs.org/api/ng/service/$location

Zima answered 1/12, 2015 at 10:41 Comment(2)
False. You don't "have to use"; You can use. It's not mandatory to use $location just because it's a service. Depending on the situation, you use window.location without a problem.Registrant
See Jim Grimmett's answer. It seems to be more accurate.Dandle
M
3

It is worth noting the current documentation for $location. Specifically the quote:

When should I use $location?

Any time your application needs to react to a change in the current URL or if you want to change the current URL in the browser.

What does it not do?

It does not cause a full page reload when the browser URL is changed. To reload the page after changing the URL, use the lower-level API, $window.location.href.

If you need to redirect the browser to a new page, $window.location is definitely recommended.

Marpet answered 16/1, 2017 at 10:5 Comment(0)
P
1
Try this,

var homeApp = angular.module('HomeApp', []);

homeApp.controller('HomeController', function ($scope, $http, $window) {
    $scope.loginname = "Login To Demo App";
    $scope.login = function () {

        var name = $scope.username;
        var pwd = $scope.password;
        var req = {
            method: 'POST',
            url: '../Account/LoginAccount',
            headers: {
                'Content-Type': undefined
            },
            params: { username: name, password: pwd }
        }

        $http(req).then(function (responce) {

            var url = '';
            if (responce.data == "True") {
                url = '../Account/WelcomePage';
                $window.location.href = url;
            }
            else {
                url = '../Account/Login';
                $window.location.href = url;
            }
        }, function (responce) {

        });
    }
});
Policewoman answered 11/4, 2016 at 7:17 Comment(1)
Maybe your code is not working, because you are not declaring $window in the controller parametersPolicewoman
H
0

Angular has its own location handler named $location which I prefer to use in angular app;

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

inject to your controller and use as following;

$location.path('http://foo.bar')
Hygrophilous answered 1/12, 2015 at 10:31 Comment(0)
H
0

You can use $window.location.href in AngularJS

app.controller('MainCtrl', function ($scope,$window) {
    $scope.formData = {};
    $scope.submitForm = function (formData){
    $window.location.href = "jobs";
    };
  })
Himation answered 24/3, 2017 at 7:57 Comment(0)
G
0

My two cents -

I could not get $window.location.href or $location.path to work to navigate away from a page. In the documentation for $location (under caveats) it says:

The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href.

$location Caveats

Documentation for $window is... lacking. Regardless, neither of the services worked for me in the controller (though $location.path works in my index.run file).

In this project I am using ui-router, so doing this worked: $state.go('state.name'); -- where state.name is the string of the state/page name to which the user wants to go, i.e. 'index.users'

Gradualism answered 6/5, 2017 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.