Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent AngularJS to return promise with cached data
    primarykey
    data
    text
    <p>I'm using a service in order to share data between controllers. However, the service is returning a promise with cached data even when making new requests. Depending on where the <code>defer</code> instance is created either live data is returned but two-way binding breaks or the two-way binding works but cached data is returned.</p> <p>How can one prevent the return of a promise with cached data and keep two-way binding? </p> <p>I've put up a plunker to illustrate the case: <a href="http://plnkr.co/edit/SyBvUu?p=preview" rel="nofollow">http://plnkr.co/edit/SyBvUu?p=preview</a> and for sake of completeness, here is the troublemaking service:</p> <pre><code>app.service('myService', function($http, $q) { // When instancing deferred here two way binding works but cached data is returned var deferred = $q.defer(); this.get = function(userId) { // When instancing deferred here two way binding breaks but live data is returned //var deferred = $q.defer(); console.log('Fetch data again using id ', userId); var url = userId + '.json'; $http.get(url, {timeout: 30000, cache: false}) .success(function(data, status, headers, config) { deferred.resolve(data, status, headers, config); }) .error(function(data, status, headers, config) { deferred.reject(data, status, headers, config); }); return deferred.promise; }; }); </code></pre> <p>UPDATE: The problem wasn't that data was cached, it was that I hadn't understood how a data was to be shared and that the shared data can not be a primitive. See my own answer below.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload