2

I have a quiz type application with a factory that gets a list of questions from the server:

xoryApp.factory('questionService', function($http) {  
  return {
  qdata : function(callback) {
      $http.get('/static/question.json').success(callback);
  }
};
});

Then I call this factory in my controller like this:

questionService.qdata(function(results) {
    $scope.qdata = results;
});

Then I switch back and forth between question and answer partial views as I loop through the questions. The problem is that every time the question view loads, it re-loads the factory json from the server. But I want it to only do that once when I load the app, not every time I load a partial view that uses that controller.

What is the way that you achieve that in angular?

Thanks

3
  • in call questionService.qdata inside a function? Or this code is placed in the controller root? Commented Oct 29, 2013 at 2:01
  • its in the root of the controller Commented Oct 29, 2013 at 2:03
  • Every times that teh controller is loaded, it will call this function because the root code will be executed. You need to put inside a function and call conditionally. I will create a plunker for you now, 5 minutes =) Commented Oct 29, 2013 at 2:07

2 Answers 2

4

Cache the results of the server call inside the service method and you are good to go

xoryApp.factory('questionService', function($http) {
var questions;
  return {
  qdata : function(callback) {
      if(questions) {
          callback(questions);
      }
      else {
        $http.get('/static/question.json').success(function(data) {
         questions=data;
         callback(questions);
        });
     }

  }

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

Comments

2

THis happens because you are calling the factory inside the controller root function. Everytime that the controller was loaded, it will call the factory again.

I propose this plunker as solution. Maybe it's not the best, but works fine.

In this case I save the list instance on a variable inside the factory, and will loaded just once. If you need to refresh, then you can call a different function to force the refresh to the server-side.

Here a couple of possible solutions:

app.factory('questionService', function($http) {  
  var result;
  return {
      qdata : function(callback) {
          return result;
      },
      fecthData : function(callback) {
          result = $http.get('question.json').success(callback);
          return result;
      }
};
});

app.factory('questionService2', function($http) {  
  var result;
  return {
      qdata : function(callback) {
          if (!result) {
              result = $http.get('question2.json').success(callback);  
          }
          return result;
      },
      refreshQdata : function (callback) {
         return $http.get('question2.json').success(callback);  
      }
};
});

I hope it helps.

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.