1

I have a controller called MoviesListsCtrl that applied using ng-controller on 3 elements in the html document.

If I use the $http service inside the controller, it will be executed 3 times for the same data.

What is the best way to retrieve a json data with ajax once and use it for the controller.

4
  • why you are having one controller for each three elements ? I mean you can apply controller to the top in which three elements lies Commented Feb 16, 2015 at 18:20
  • 2
    Well, all work with $http, $reource etc. should be in services. If u get i.e. user name (same in all application), then you just write UserService. If u get i.e. some item name (by id), then it is convenient to store this value in parent scope (and still get it using service). Commented Feb 16, 2015 at 18:22
  • Because the 3 elements use the data or every element takes what it needs from the data. Commented Feb 16, 2015 at 18:22
  • @Petr Averyanov: how can I do that ? How the the data will be accessed by the controller after the service getting it using $http ? jsfiddle.net/jnkk7h6p/1 Commented Feb 16, 2015 at 18:28

2 Answers 2

3

If its a GET request, then you can cache the response so only one call is made, and all subsequent calls will pull from cache.

$http({
    url: "someUrl",
    cache: true
}).success(function(){
     // do something
}).error(function(){
     // do something else
});

Your code will still run multiple times since you have it on each element, but it will only make one http call.

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

Comments

2

Use an Angular service or factory. You can abstract away the $http call inside your service, so that your controllers always use the same interface.

Something like:

angular.module('myModule')
    .service('myService', function ($http, $q) {
        var myData = null;

        this.get = function (params) {
            return myData 
                ? $q.when(myData) 
                : $http.get('http://example.com/myEndpoint', params)
                    .success(function (data) {
                        myData = data;
                    });
        };
    });

This is rudimentary, but it fits the general idea. (You probably want to hold a reference to your promise and pass that back to the callee, so that multiple requests that occur before the promise has resolved don't cause new HTTP requests.)

After this, you can inject the service into your controller, and simply call it to get the data you need. The nice thing about this is that you will always call the same method to get your data. Hope this helps, good luck.

2 Comments

I think it still makes 3 requrests
@faressoft This code snippet is just an example - as I said, it's rudimentary. However, you can use the idea behind it to build your own service.

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.