Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am fairly new to angularjs, so please bear with me.

I have two controllers, one of which has a Restangular call to load a certain json object. How do I access this same json object in another controller without making another Restangular call?

I already tried making a factory that shares the object, but this is far from working.

app.factory('Data', function() {
    return []
}

app.controller("FirstCtrl", function ($scope, AudioRestangular, Data) {
    $scope.audios_full = Data
    // load data into $scope.audios_full using restangular
}

app.controller("SecondCtrl", function ($scope, AudioRestangular, Data) {
    /*
     * $scope.second_ctrl_audios = ?
     * get data from FirstCtrl's audios_full
     * and store it in this $scope.second_ctrl_audios
    */
}
share|improve this question
    
possible duplicate of Can one controller call another in AngularJS? –  Stewie Nov 21 '13 at 12:15
    
Show the code that is used to fill the data? –  Chandermani Nov 21 '13 at 12:17
add comment

1 Answer

You can go down the custom service / factory route, to effectively create your own caching layer as you suggest, or you can enable caching in Restangular. From https://github.com/mgonto/restangular#can-i-cache-requests

RestangularProvider.setDefaultHttpFields({cache: true});

You can probably put this in the "run" call of the app, like so:

app.run(function(RestangularProvider) {
  RestangularProvider.setDefaultHttpFields({cache: true});
});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.