mercredi 2 septembre 2015

How to handle $http asynchronous issues in angularjs factory service?

In the factory service js file, I created a service like this:

DashboardFactory.restService = function(method) {
var teststatus = "aaa";
switch (method) {

case "getAll":

    $http.get($_REST_API_ROOT_URL + "clients").success(function(data) {
        teststatus = data;
    }).error(function(error) {
        teststatus = 'Unable to load the client data: ' + error.message;
    });
    teststatus = "bbb";
    break;

}
return teststatus;
};

In controller, the code is like this:

$scope.AllClients=DashboardFactory.restService("getAll","","");

I also put the "AllClients" on the html page to monitor the result:

{{AllClients}}

I think the "AllClients" should show the API data from the remote server. But in reality it always give me "bbb".

What should I do?

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire