Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS TypeError is not a function

I have the following code in my service:

testApp.service('detailsService',['databaseService', 'loggedService', '$http', function(databaseService, loggedService, $http){

    var details;

    this.getDetails = function(name){

        return $http({
            method : "GET",
            url : name    
        }).then(function(response) {
           details= response.data;
           console.log(response.data);
           return response.data;
        });

    };

}]);

What i want to do is call this function in my controller when the page(view) is loaded.

testApp.controller('testController', ['$scope', '$location', 'databaseService','detailsService', '$routeParams', function($scope, $location, databaseService, $routeParams, detailsService){

    $scope.details;

    var selectedDetails = function(name){
             detailsService.getDetails(name).then(function(data){
            $scope.details= data;
        });
    };

    selectedDetails(name);

}]);

I keep getting the error detailsService.getDetails is not a function. I'm using the same function from the detailsService in another controller without any problems.

Does anybody know why i keep getting this error?

like image 600
Urban Avatar asked Mar 21 '17 09:03

Urban


1 Answers

The error is expected as you are not injecting dependencies properly, You need to use the correct sequence.

testApp.controller('testController', ['$scope', 
    '$location', 
    'databaseService',
    'detailsService', 
    '$routeParams', 
    function($scope, $location, databaseService, detailsService, $routeParams ){

instead of

testApp.controller('testController', ['$scope', 
    '$location', 
    'databaseService',
    'detailsService', 
    '$routeParams', 
    function($scope, $location, databaseService, $routeParams, detailsService){

Note Both the string part and the function arguments need to match up 1:1.

like image 171
Satpal Avatar answered Oct 26 '22 21:10

Satpal