How to clear template cache?
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.