Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

angular - How to clear template cache?

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]
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...