1

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>
1
  • Don't edit your question's code to match the improvements people suggest in their answers.. Now everyone who views this question for the first time thinks: 'Dafuq, everything ppl suggest is allready in his code..' Commented Jun 8, 2015 at 9:56

2 Answers 2

3

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

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

Comments

2

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

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.