In Angular 2, how do you clear the template cache? There are tons of answers for Angular 1, but none for 2.
The issue I am having is that when I change the contents of the html pages referenced by templateUrl on any components, the html pages don't change in the browser until I manually navigate to the templateUrl in the browser and hit reload. I know you can disable the browser cache to solve this during development, but my concern is that users can see an outdated html page if they have it cached in their browser when I go to update a website with Angular 2.
Here is a link to the stack overflow questions for Angular 1 AngularJS disable partial caching on dev machine
Below is a snippet and I am having issues with app.html updating when its content is changed.
@Component({
selector: 'photogallery-app',
templateUrl: './app/app.html',
directives: [ROUTER_DIRECTIVES, CORE_DIRECTIVES]
})
First import the TemplateCompiler.
import { TemplateCompiler } from 'angular2/src/compiler/template_compiler';
Next inject the TemplateCompiler in your constructor.
constructor(private _templateCompiler: TemplateCompiler)
Finally use that to clear the cache. Note this clears all templates.
this._templateCompiler.clearCache();
UPDATE: Angular 2 Beta 17
First import the RuntimeCompiler.
import { RuntimeCompiler} from 'angular2/src/compiler/runtime_compiler';
Next inject the RuntimeCompiler in your constructor.
constructor(private _runtimeCompiler: RuntimeCompiler)
Finally use that to clear the cache. Note this clears all templates.
this._runtimeCompiler.clearCache();
UPDATE: Angular 2 RC 1
First import the RuntimeCompiler.
import { RuntimeCompiler} from '@angular/compiler/src/runtime_compiler';
Next inject the RuntimeCompiler in your constructor.
constructor(private _runtimeCompiler: RuntimeCompiler)
Finally use that to clear the cache. Note this clears all templates.
this._runtimeCompiler.clearCache();
UPDATE: Angular 2 RC 4
First import the RuntimeCompiler.
Notice the path change from RC1. The path listed for RC1 will throw errors when calling .ClearCache() if used with RC4
import { RuntimeCompiler} from '@angular/compiler';
Next inject the RuntimeCompiler in your constructor
constructor(private _runtimeCompiler: RuntimeCompiler)
Finally use that to clear the cache. Note this clears all templates.
this._runtimeCompiler.clearCache();
UPDATE: Angular 2.0.0 (RTM)
It cannot be done. I have an app that serves one set templates for logged in users, and another set for those not logged in. After upgrading to 2.0.0, I can see no way to accomplish the same task. While I try to figure out the best way to re-architect the application, I have resorted to this instead:
location.reload();
That works (but obviously reloads the entire page).
In Angular 2.0.2 we can clear the template cache as follows:
Import:
import { Compiler } from '@angular/core';
Dependency Injector:
constructor(private _compiler: Compiler) {
}
Usage:
this._compiler.clearCache();
Note: I believe that this solution works from Angular 2.0.0 (RTM)
Update
In current versions (4.x) of Angular
clearCache()for is no longer working. This has already been reported in the Angular Github (by @Olezt) and possibly will be corrected in the future.
Explanation of functional purpose of clearcache() method:
Another question that is unclear is the functional purpose of the clearCache() method, clearCache() has the responsibility of removing the templateUrl, stylesUrl, etc. specified in @Component. (I use it to discard the view loaded in templateUrl and request a updated view of the server, in my project the view resulting from this request is changed according to exchange user/permissions for example. So the need to discard the one previously loaded.)
clearCache() has no action on the browser cache, or anything beyond the Angular, it only acts on the @Component, @NgModule, etc. Angular caches, nothing more.
In Angular 5.X we can use the compiler in the same way as 2.0.
Import the compiler:
import { Compiler } from '@angular/core';
Inject the Dependency in your component constructor :
constructor(private _compiler: Compiler) { }
Clear the cache:
this._compiler.clearCache();