2

I'm trying to move all my AJAX from an Angular controller into a service. My code worked fine in the controller, but I can't get the same $http requests to run in the service. When I run the following code, Data.tracker is set to "function started" and then never changes.

app.service("Data", function($http) {

this.tracker = "not started";

this.loadHumanReadableNames = function() {
        this.tracker = "function started";
        $http.get('http://proteomics.ysu.edu/secretomes/animal/index.php/api/get_human_readable_names/').
        success(function(data, status, headers, config) {
            this.tracker = "ajax successful";
        }).
        error(function(data, status, headers, config) {
            this.tracker = "ajax failed";
        });
    };
});

app.controller('GlobalController', function($scope, Data) {

    $scope.Data = Data;

    $scope.initializeData = function () {
        //load human readable names
        Data.loadHumanReadableNames();
    }
});

Any ideas?


UPDATE

MajoB's answer works great. However, for modularity I generally prefer to handle data processing in my model (service) instead of in my controller. The problem with the above code is that this is not available in the success and error functions. The following will print "ajax successful":

in the service:

app.service("Data", function($http) {

this.tracker = "not started";
var self = this;

this.loadHumanReadableNames = function() {
        this.tracker = "function started";
        $http.get('http://proteomics.ysu.edu/secretomes/animal/index.php/api/get_human_readable_names/').
        success(function(data, status, headers, config) {
            self.tracker = "ajax successful";
        }).
        error(function(data, status, headers, config) {
            self.tracker = "ajax failed";
        });
    };
});

2 Answers 2

1

this keyword inside success callback function refer the local scope not the parent object. So it won't change . Try to assign with some variable after that you can change that variable inside success callback function

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

1 Comment

services/providers are singletons (one sevice instance per application) they do not share any parent scope, they can only access a $rootScope if it is included
0

you can return a promise from your service:

this.loadHumanReadableNames = function() {            
        return $http.get('http://proteomics.ysu.edu/secretomes/animal/index.php/api/get_human_readable_names/');            
    };
});

controller

app.controller('GlobalController', function($scope, Data) {   


    $scope.initializeData = function () {
        //load human readable names
        Data.loadHumanReadableNames().success(function (data, status, headers, config) {

        });;
    }
});

or create a custom promise with $q:

    this.loadHumanReadableNames = function() {     
       var deferred = $q.defer();
       $http.get('http://proteomics.ysu.edu/secretomes/animal/index.php/api/get_human_readable_names/').success(function (data, status, headers, config) {
            deferred.resolve({ data: data, tracker : 'ajax successful' })
       });            


       return deferred.promise;        
    };

controller:

app.controller('GlobalController', function($scope, Data) {   


        $scope.initializeData = function () {
            //load human readable names
            Data.loadHumanReadableNames().then(function (data) {

            });;
        }
    });

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.