0

Hey guys i'm new to angular and am not very proficient with javascript. This setup pulls in the json data fine, however when I make changes to some of the properties in the object, they reset when I change views and the controller is reloaded. Any help or guidance on how to approach this would be appreciated.

app.controller('MainCtrl', function ($scope, $location, Quiz) {
    $scope.quiz = {};

    Quiz.getQuestions(function(data) {
      $scope.quiz = data;
    });
});

app.service('Quiz', function($http) {
  this.getQuestions = function(callback) {
    $http.get('questions/questions.json').success(function(data) {
      if (callback) {callback(data);}
      return data;
    });
  };
});
jdeutsch
  • 3
  • 1
  • Example of how to use a service to share/persist data across controllers: http://stackoverflow.com/questions/11112608/angularjs-where-to-put-model-data-and-behaviour – Rob J Dec 18 '13 at 01:51

1 Answers1

0

Does the $http get request get repeated on subsequent calls to getQuestions() overwriting the object? If so, perhaps

app.service('Quiz', function($http) {
  var _data;
  this.getQuestions = function(callback) {
    if (_data) {
       callback(_data); 
    }
    $http.get('questions/questions.json').success(function(data) {
      _data = data;
      if (callback) {callback(data);}
      return data;
    });
  };
});
prototype
  • 7,249
  • 15
  • 60
  • 94