0

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
    */
}
2

1 Answer 1

1

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});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Not run() function but I think config() function of the app.

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.