Sajeetharan
Sajeetharan

Reputation: 222582

Cannot read property 'get' of undefined with Angular

I am trying to build a simple authentication using angular, it gives this error when making a ajax service call. Here is the code.

Code

mainModule.controller("loginController", ['$scope', '$http', '$rootScope', '$presence', '$apps', '$helpers',
    function($scope, $http, $rootScope, $presence, $apps, $helpers) {
        $scope.currentUser;
        $scope.password;
        $rootScope.url = "http://localhost:53455/eSuperVision.svc";
        $scope.login = function($scope, $http) {
            $http.get($rootScope.url + "/login/" + $scope.currentUser + "/" + $scope.password).success(function(response) {
                if (response == true) {
                    location.href = "home.html";
                } else {
                    $("#loginDialog").effect("shake");
                    $scope.showError = true;
                }
            });
        }
    }
]);

Upvotes: 1

Views: 544

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

You function should not pass arguments of $http & $scope, they are getting overridden by a function and both of them got undefined. you should use the dependency which you have injected from controller.

Code

$scope.login = function() {
  $http.get($rootScope.url + "/login/" + $scope.currentUser + "/" + $scope.password).success(function(response) {
    if (response == true) {
      location.href = "home.html";
    } else {
      $("#loginDialog").effect("shake");
      $scope.showError = true;
    }
  });
}

Upvotes: 1

cuttlas
cuttlas

Reputation: 981

Because the $http you are using in your login fuction is not the $http service injected in the controller but an arbitrary param passed to the function.

Just remove it: $scope.login = function() {...}

Upvotes: 2

Related Questions