I have problem with caching partials in AngularJS.
In my HTML page I have:
<body>
<div ng-view></div>
<body>
where my partials are loaded.
When I change HTML code in my partial, browser still load old data.
Is there any workaround?
I have problem with caching partials in AngularJS. In my HTML page I have:
where my partials are loaded. When I change HTML code in my partial, browser still load old data. Is there any workaround? |
||||
For Development you can also deactivate the browser cache - In Chrome Dev Tools on the bottom right click on the gear and tick the option
Update: In Firefox there is the same option in Debugger -> Settings -> Advanced Section (checked for Version 33) Update 2: Although this option appears in Firefox some report it doesn't work. I suggest using firebug and following hadaytullah answer. |
|||||||||||||||||||||
|
Building on @Valentyn's answer a bit, here's one way to always automatically clear the cache whenever the ng-view content changes:
|
|||||||||||||||||||||
|
As mentioned in the other answers, here and here, the cache can be cleared by using:
However as suggested by gatoatigrado in the comment, this only appears to work if the html template was served without any cache headers. So this works for me: In angular:
You may be adding cache headers in a variety of ways but here are a couple of solutions that work for me. If using
If using Nginx, you can add this to your config:
Edit I just realised that the question mentioned |
|||||
|
If you are talking about cache that is been used for caching of templates without reloading whole page, then you can empty it by something like:
And in markup:
And press this button to clear cache. |
|||
|
Solution For Firefox (33.1.1) using Firebug (22.0.6)
|
|||||
|
I'm posting this just to cover all possibilities since neither of the other solutions worked for me (they threw errors due angular-bootstrap template dependencies, among others). While you are developing/debugging a specific template, you can ensure it always refreshes by included a timestamp in the path, like this:
Note the final |
|||||||||||||||||||||
|
This snippet helped me in getting rid of template caching
The details of following snippet can be found on this link: http://oncodeanddesign.blogspot.com/2014/02/safely-prevent-template-caching-in-angularjs.html |
|||||||||
|
As others have said, defeating caching completely for dev purposes can be done easily without changing code: use a browser setting or a plugin. Outside of dev, to defeat Angular template caching of route-based templates, remove the template URL from the cache during $routeChangeStart (or $stateChangeStart, for UI Router) as Shayan showed. However, that does NOT affect the caching of templates loaded by ng-include, because those templates are not loaded through the router. I wanted to be able to hotfix any template, including those loaded by ng-include, in production and have users receive the hotfix in their browser quickly, without having to reload the entire page. I'm also not concerned about defeating HTTP caching for templates. The solution is to intercept every HTTP request that the app makes, ignore those that are not for my app's .html templates, then add a param to the template's URL that changes every minute. Note that the path-checking is specific to the path of your app's templates. To get a different interval, change the math for the param, or remove the % completely to get no caching.
|
|||||||||||||||||||||
|
If you are using UI router then you can use a decorator and update $templateFactory service and append a query string parameter to templateUrl, and the browser will always load the new template from the server.
I am sure you can achieve the same result by decorating the "when" method in $routeProvider. |
|||||
|
There is no solution to prevent browser/proxy caching since you cannot have the control on it. The other way to force fresh content to your users it to rename the HTML file! Exactly like https://www.npmjs.com/package/grunt-filerev does for assets. |
|||
|
Here is another option in Chrome. Hit F12 to open developer tools. Then Resources > Cache Storage > Refresh Caches. I like this option because I don't have to disable cache as in other answers. |
|||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
app.config.update(SEND_FILE_MAX_AGE_DEFAULT=0)
to myflask_app.py
. (I imagine similar things exist for other web servers). – gatoatigrado Aug 13 '13 at 3:38Ctrl+Shift+R
(i.e. Hard Reload) and no matter what caching mechanism is used chrome will ignore it and re-fetch all the scripts, stylesheets etc. – snajahi Sep 18 '14 at 15:15ng-include
|ng-view
|templateUrl
are not handled by this shortcut – Werlang May 14 '15 at 22:36