0

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!

2 Answers 2

0

teststatus gets set as "bbb" and is returned before the API call is finished since the call is asynchronous.

Refer to AngularJS $http call in a Service, return resolved data, not promises for the different ways you can return the data from your API call.

What you want to do is return teststatus at the end of your .success()/.error() functions rather than outside. That way, it only returns once it finishes the API call and sets it to the data returned by the call.

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

4 Comments

That is exactly what I want to do. How do I do it then? I am very new to angularjs.
Remove the return teststatus; line at the end of your function. Then add return teststatus; after each time you assign teststatus a value inside your success() and error() functions.
That will return them from the success method but not from the service.
Sorry, I overlooked that. Refer to stackoverflow.com/questions/27238928/… to return the data from your call.
0

Method 1: You don't NEED to return it at all. You could send in your model to the service RestService.getAllClients = function(myModel), set a property on the model on success myModel.AllClients = data; and then display it with {{myModel.AllClients}}. Setting the property directly on the passed in model will update the binding automagically.

Example: http://jsbin.com/muxijozofa/edit?html,js,output

Method 2: Otherwise you'd need to return the entire get call, which will return a promise, which you'll then need to resolve on the controller as per f.e How do I return data from a $http.get() inside a factory in angularjs

Refactoring tip: Instead of building a "catch-all" rest service filled with switch-case:s, you could build a general rest-service factory which you then implement for each type of call. So you get one method per type of rest call. Instead of DashboardFactory.restService, you'd then call RestService.getAllClients, which sets the data or returns a promise per the methods above.

Switch-case is just bad for feature selection, you're hiding functionality and making the code prone to hidden errors.

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.