Clear the cache following the service call

I am currently working on a service that has two methods. Utilizing the built-in cache support for $resource, I am trying to implement a way to refresh the cache when a service call is made from the controller. I attempted to use $cacheResource without success. Below is my code:

Service

angular.module('module').factory('Service1',['$resource',
   function ($resource){
     return $resource('..api/latest/myservice', {}, {

       takeSomething: {
         method: 'GET',
         url: '..api/latest/myservice/takesomething',
        cache: true
       },

       putSomething: {
         method: 'GET',
         url: '..api/latest/myservice/putsomething',
         cache: true
       }

      }
}]

Controller

angular.module('module').factory('MyController',['$scope','Service1',
     function($scope, service1) {
       ....

     $scope.update = function() {
       service1.putSomething({
       ......
       refresh cache for takeSomething method in service or for all methods
     });
     }

}

When the update function is called from the controller, I aim to refresh the cache for the takeSomething method or for all methods.

Answer №1

If you want to access the default $http cache, you can do so by utilizing the $cacheFactory function which includes a remove method for your needs.

To make use of $cacheFactory, remember to inject it into your code where necessary, such as in your $scope.update or consider moving it to your service for better organization.

var $httpDefaultCache = $cacheFactory.get('$http');

$httpDefaultCache.remove('..api/latest/myservice/takesomething');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript: Generating multiple variables using a for loop?

Is there a way to dynamically create n variables a_1, a_2, a_3 ... a_n, where the value of n is determined during runtime? Attempting to use the following code would not produce the desired outcome: var n = prompt("Enter number of variables?"); for (i= ...

While using jQuery, it was discovered that the name attribute could be changed within the .each() function when working in console.log. However

I'm attempting to modify the name attribute of a cloned table row with the following code: var ipCount = 2; $("#input_form").on("click", "#add_input_param", function() { $('#input_param tr').eq(1).clone().find('input').val ...

Confirming angular 1.x input checkbox

In my code, I have an angular 1.x checkbox set up like this: <input type="checkbox" name="fooName" id="fooId" ng-model="false" > When I use jQuery to do the following: $("#fooId").val() I always receive "on" as the output. The same result oc ...

Implementing event dispatch on Push notifications received with Workbox

In my VueJS component, I have set up a listener for the pushMessageEvent event: <template> <div> <VueBotUI :options="options" :is-open="isOpen" :bot-typing="botTyping" :inpu ...

React-Redux is throwing a TypeError due to its incapability of reading the property 'map' which is undefined

I'm still fairly new to diving into React and Redux, and I find myself a bit puzzled. My main objective is to populate HTML with a plethora of JSON data through GET requests. Utilizing react and redux to control the state of these objects, however, i ...

In relation to the Uncaught Error: Syntax error, an unrecognized expression has been encountered

Recently, I started working on an AngularJS and Node.js application. It's all new to me. In the HTML page, I defined a link as <li><a data-toggle="modal" data-target="#myModal" href="/#/login">Login</a></li>, and then set up th ...

`When you extend a $resource object, it effectively removes the prototype from the

Here's the current code snippet I'm working with: // Factory angular.factory('Student', function() { var defaults = { StudentId: null }; var Student = function(params) { angular.extend(this, angular.copy(de ...

Menu becomes sticky too quickly on iOS Safari and Chrome, jumping to fixed position prematurely

Feeling frustrated with this seemingly straightforward code challenge. My sticky menu is causing me headaches on iOS devices, particularly the iPhone 6 running the latest iOS. It seems like the menu jumps into its fixed position too early, leading me to be ...

Employed Yeoman for Angular project setup using Gulp, facing issues with Protractor functionality

Subject pretty much explains it all. I am using OSX Yosemite and tried updating webdriver, chromedriver, and PhantomJS. Unfortunately, I keep getting this error: [15:41:30] Starting 'inject'... [15:41:30] gulp-inject 1 files into index.html. [15 ...

What is the best way to show only one div at a time when selecting from navbar buttons?

To only display the appropriate div when clicking a button on the left navbar and hide all others, you can use this code: For example: If "Profile" is clicked in the left navbar, the My Profile Form div will be displayed (and all others will remain hidde ...

To successfully display the data on my ChartJS Line graph, I have to first click on the color legend

I am currently using Chart JS to visualize sensor data retrieved from a firebase firestore database. I've come across an unusual issue where my chart fails to display the data initially >> https://i.sstatic.net/qD6Sd.jpg but after performing two ...

What could be causing the error when attempting to retrieve an index using the window variable?

I'm facing a strange issue where I am trying to utilize a variable that I define as follows: window.params['variable'] = { ... }; Within the function that I am using, the code looks like this: function q() { ... // for example return n ...

Learn how to transform a raw readme file into an HTML formatted document using AngularJS, after retrieving it from GitHub

Can someone help me figure out how to format each line of the README.MD raw file into an HTML document using the controller below? angular.module('ExampleApp', []) .controller('ExampleController', function($scope, Slim,$sce) { ...

HTML forms default values preset

I need help with pre-setting the values of a dropdown menu and text box in an HTML form for my iPhone app. When the user taps a button, it opens a webview and I want to preset the dropdown menu and text field. Can someone guide me on how to achieve this? ...

When trying to use express, the error message "d3.scale is

Having trouble integrating a c3-chart into my web application using node.js and express. The c3.js file is throwing the error message: TypeError: d3.scale is undefined Here is an excerpt from my layout.yade file: doctype html html head title= titl ...

Tips on waiting for an event to be processed during a Protractor end-to-end test

I have a straightforward AngularJs 1.4.8 Front-End: https://i.stack.imgur.com/2H3In.png After filling out the form and clicking the OK button, a new person is added to the table. The form resides in the addingPersonController, while the table is control ...

Utilize different versions of jQuery along with various plugins

I have integrated the AdminLTE Template into my project, which requires jQuery Version 3.X. Additionally, I am using the Flotchart jQuery library, which specifically needs jQuery Version 1.11.3. To handle this conflict, I have implemented the $.noConflic ...

Editing input within a Bootstrap 4 popover causes it to lose focus

I am using Bootstrap 4 along with the Bootstrap colorpicker to implement a colorpicker within a popup that includes an input field for setting the color code. However, I am facing an issue where the input field (#color-value) seems uneditable when the popo ...

Updating Socket.io with multiple data emissions upon refresh or reload

I'm facing an issue that is very similar to this problem: https://github.com/rethinkdb/rethinkdb/issues/6503 Whenever I connect for the first time, it only logs once. Upon refreshing, it logs twice. Subsequent refreshes result in an additional log ea ...

The jQuery .load function does not function properly when an ajax query is already underway

My web application utilizes dynamic loading of content within the same div (.content) through either an ajax request or a .load() request. An example of the ajax request code snippet is: $(document).on('click', '.button1', functio ...