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?

1

3 Answers 3

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.

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

1 Comment

Just want to add, this question will explain what the [] are all about for those that don't know, and want to. stackoverflow.com/questions/18032068/…
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);
}]);

Comments

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

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.