9

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?

Urban
  • 585
  • 2
  • 13
  • 28
  • Possible duplicate of [Angular - TypeError: XX is not a function](http://stackoverflow.com/questions/30876487/angular-typeerror-xx-is-not-a-function) – Nikunj Sardhara Mar 21 '17 at 10:07

3 Answers3

12

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.

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Just want to add, this question will explain what the [] are all about for those that don't know, and want to. http://stackoverflow.com/questions/18032068/what-is-the-purpose-of-square-bracket-usage-in-angular – Brian Mar 21 '17 at 10:25
0

remove the promise in the service.Just return the request. since you are catching the promise from the controller no need to use it in the service

testApp.service('detailsService', ['databaseService', 'loggedService', '$http', function(databaseService, loggedService, $http) {
    var details;
    this.getDetails = function(name) {
        return $http({
            method: "GET",
            url: name
        })
    };
}]);

Also make sure the sequence is correct order when you are injecting services.

change this

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

to this

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

controller

testApp.controller('testController', ['$scope', '$location', 'databaseService', 'detailsService', '$routeParams', function($scope, $location, databaseService, detailsService , $routeParams) {
    $scope.details;
    var selectedDetails = function(name) {
        detailsService.getDetails(name).then(function(data) {
            $scope.details = data;
        });
    };
    selectedDetails(name);
}]);
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
0

I got this error because I had added a callback method to a component, but the callback was not used by all the pages where that component was used:

<component [myCallbackFn]="fnNameToBindToFn">

component.ts:

public onClick() {
    // some code
    this.myCallbackFn();
}

Each time it was not used I got this error which was effectively a NullPointerException (from Java). So in the other uses of <component> in my website I had to add a check in the component before the callback to say:

if (undefined !== this.myCallbackFn) this.myCallbackFn();
Zoe
  • 27,060
  • 21
  • 118
  • 148
Scala Enthusiast
  • 399
  • 8
  • 18