$location error in angularJS

677 views Asked by At

I have a master controller and I would like to set up the login page to take me to "/tables" path if username=admin and password=admin, however I am getting this error everytime I try to login

TypeError: Cannot read property 'path' of undefined

I added $location in the controller and in the login function but nothing changed keep getting same error. If I take $location out I get this error

ReferenceError: $location is not defined

Not sure what to do there. Any help is appreciated. Here is my code

 angular
    .module('RDash')
    .controller('MasterCtrl', ['$scope',  '$cookieStore', MasterCtrl]);




function MasterCtrl($scope, $cookieStore, $location) {
    /**
     * Sidebar Toggle & Cookie Control
     */
    var mobileView = 992;


    $scope.getWidth = function() {
        return window.innerWidth;
    };


    $scope.login = function($location) {
        if($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
            alert("you are not the admin");
        }
        else{
            $location.path('/tables');
          }

      };
}

login.html:

<div ng-controller="MasterCtrl">
<form >
  <div class="jumbotron" class="loginForm">
  <div class="form-group" id="loginPageInput">
    <label for="exampleInputEmail1">User Name</label>
    <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter Username" ng-model="credentials.username">
  </div>
  <div class="form-group" id="loginPageInput">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ng-model="credentials.password">
  </div>
  <div id="loginPageInput">
    <button ng-click="login()" type="submit" class="btn btn-primary btn-lg">Submit</button>
  </div>
</form>
  <div id="loginPageInput">
  <div class="wrap">
    <button ng-click="register()" class="btn btn-default" id="lefty" >Register</button>
    <p ng-click="forgotpass()" id="clear"><a>Forgot my Password</a></p>
  </div>
  </div>
</div>
</div>
2

There are 2 answers

0
Pankaj Parkar On BEST ANSWER

You missed to inject $location in your dependency array of MasterCtrl

Code

angular
    .module('RDash')
    .controller('MasterCtrl', ['$scope',  '$cookieStore', '$location', MasterCtrl]);
                                                          //^^^^^^^^
function MasterCtrl($scope, $cookieStore, $location) {

Also you need to remove $scope.login parameter $location which is killing the service $location variable existance which is inject from the controller.

$scope.login = function ()

0
dfsq On

You are not passing any $location to login function hence it's undefined and shadows outer $location local variable. Correct code should be:

$scope.login = function () {
    if ($scope.credentials.username !== "admin" && $scope.credentials.password !== "admin") {
        alert("you are not the admin");
    } else {
        $location.path('/tables');
    }
};