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>

4
  • Try withouth the $ sign. window.location.href = "[email protected]" Commented Dec 1, 2015 at 10:23
  • Use basic Javascript window redirect .. that is without $. window.location.href. Commented Dec 1, 2015 at 10:24
  • It worked. Thank you :) But why doesn't it work with the dollar sign ? Commented Dec 1, 2015 at 10:25
  • @VarunKN check this link. Commented Dec 1, 2015 at 10:28

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
See Jim Grimmett's answer. It seems to be more accurate.
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.

Comments

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

        });
    }
});

1 Comment

Maybe your code is not working, because you are not declaring $window in the controller parameters
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";
    };
  })

Comments

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

Comments

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.