TypeError:AuthenticateService.checkUser不是函数

时间:2016-10-26 15:53:32

标签: angularjs

TypeError:AuthenticateService.checkUser不是函数

I am submitting a login page with credentials, for sending the credentials to the server I have service **AuthenticateService** with $http.post, calling the method in the above mentioned service from the contactLoginCtrl.js (controller),
during that I am encountering this error

下面是 AuthenticateService

的代码
  angular.module('app.services')
.service('AuthenticateService',['$http',function($http){

    return{         

        checkUser : function(uname, pword){

             var indata = { "password": pword, "username":uname };
             var req ={
                          method: 'POST',
                          url: 'http://localhost:8083/spring2/login',
                          data: indata
                      }

                  $http(req).then(function (response) {
                      console.log(response);
                      return response;
                  }, function (error) {
                      console.log(error);
                      return error;

                  });
                }

            }

}]); 

下面是控制器代码contactLoginCtlrls.js

angular.module('app.controllers')
.controller('contactLoginCtrl',['$rootScope','$scope','AuthenticateService','$location',function($scope,AuthenticateService,$location){

    $scope.authenticate =  function(userModel){
        var userName = userModel.username;
        var password = userModel.password;
        console.log(userName+password);

         ***AuthenticateService.checkUser(userName,password);***
    };

}])
 The above highlighted line is causing the error could some one please check this?


----------------------ignore the below section------------------------

添加的内容仅用于从stackoverflow跳过错误以添加更多细节

2 个答案:

答案 0 :(得分:1)

您尝试向控制器'$rootScope', '$scope', 'AuthenticateService', '$location'注入四个服务,但您只在函数参数中声明了其中三个。

而是尝试:

angular.module('app.controllers')
       .controller('contactLoginCtrl', ['$rootScope', '$scope', 'AuthenticateService', '$location', 
        function($rootScope, $scope, AuthenticateService, $location)
        {...}

答案 1 :(得分:1)

您的控制器没有$rootScope作为参数注入。像这样改变,

angular.module('app.controllers')
.controller('contactLoginCtrl',['$rootScope','$scope','AuthenticateService','$location',function($rootScope,$scope,AuthenticateService,$location){