4

I'm trying to follow the accepted answer here, and make a call to RuntimeCompiler.clearCache()

Here's how I've tried to do it:

import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
import { RuntimeCompiler } from '@angular/compiler';

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
})

export class AppComponent implements OnInit {
    constructor(private _runtimeCompiler: RuntimeCompiler) {}

    ngOnInit() {
        this._runtimeCompiler.clearCache();
    }   
}

But I'm getting this error:

 ORIGINAL EXCEPTION: No provider for RuntimeCompiler!

What am I missing to here?

a better oliver
  • 26,330
  • 2
  • 58
  • 66
Juicy
  • 11,840
  • 35
  • 123
  • 212

4 Answers4

2

With RC5+ this providers should be registered on a AppModule level

@NgModule({
    imports: [
        BrowserModule
        ...
    ],
    declarations: [ ... ],
    bootstrap:    [ ... ],
    providers: [
        COMPILER_PROVIDERS
    ],
})
export class AppModule { }

Check this How can I use/create dynamic template to compile dynamic Component with Angular 2.0? for a working plunker

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
2

Remove the line

import { RuntimeCompiler } from '@angular/compiler';

Then add Compiler to import @angular/core. And replace RuntimeCompiler to Compiler;

import { Component, Compiler } from '@angular/core';
import { OnInit } from '@angular/core';


@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
})

export class AppComponent implements OnInit {
    constructor(private _compiler: Compiler) {}

    ngOnInit() {
        this._compiler.clearCache();
    }   
}

https://angular.io/docs/ts/latest/api/core/index/Compiler-class.html

Alexandre N.
  • 2,594
  • 2
  • 28
  • 32
1

Add RuntimeCompiler to your component providers. (providers: [RuntimeCompiler], below templateUrl)

Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
  • Thanks for the answer. When I add the `providers` I now get: `No provider for CompileMetadataResolver! `. – Juicy Sep 02 '16 at 13:26
1

Add RuntimeCompiler as provider in your component.

import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
import { RuntimeCompiler } from '@angular/compiler';

@Component({
   moduleId: module.id,
   selector: 'my-app',
   templateUrl: 'app.component.html',
   providers: [RuntimeCompiler]
})

export class AppComponent implements OnInit {

  constructor(private _runtimeCompiler: RuntimeCompiler) {}

  ngOnInit() {
      this._runtimeCompiler.clearCache();
  }   
}
Kanso Code
  • 7,479
  • 5
  • 34
  • 49
  • Thanks for the answer. When I add the `providers` I now get: `No provider for CompileMetadataResolver! `. – Juicy Sep 02 '16 at 13:26
  • @Juicy, which version of ng do you use? according to official api, is there is like simple `Compiler` class without `Runtime`, check (this link)[https://angular.io/docs/js/latest/api/core/index/Compiler-class.html] – Kanso Code Sep 02 '16 at 15:47
  • Thanks for pointing that out. I was looking in to it. It does have the `clearCache` method but I'm not sure how to use this... `import {Compiler} from '@angular/compiler` tells me that there is no exported member called `Compiler`. I'm using latest `@angular` from `npm` which I think is `RC5`. – Juicy Sep 02 '16 at 16:19
  • 1
    @Juicy, try to import {Compiler} from '@angular/core – Kanso Code Sep 02 '16 at 17:14