In angular5, I'm trying to use the same AOT compilation of a project for most of my module / component ... but I have one part that needs to be compiled by JIT.
For this second part, the HTML comes from an Ajax request and contains the component tag that must be compiled by angular. To manage this part, I use a directive that looks like this:
export class ArticleLiveDirective implements OnInit, OnChanges, OnDestroy {
constructor(
private container: ViewContainerRef,
private compiler: Compiler
) { }
private addHtmlComponent(template: string, properties: any = {}) {
this.container.clear();
const divTag = document.createElement('div');
divTag.setAttribute('id',this.currentId);
divTag.innerHTML = template;
template = divTag.outerHTML;
@Component({ template })
class ArticleLIveComponent implements OnInit, OnChanges, OnDestroy {
constructor(
private articleService: ArticleService
) {}
ngOnInit() {}
ngOnChanges(changes: SimpleChanges) {}
ngOnDestroy() {}
goToPage($event: Event, pagination: string) {
this.articleService.askToChangeArticle(pagination);
$event.stopPropagation();
return false;
}
}
@NgModule({
declarations: [
ArticleLIveComponent
],
imports: [
BrowserModule,
MatTabsModule
],
providers: []
})
class ArticleLiveModule {}
const mod = this.compiler.compileModuleAndAllComponentsSync(ArticleLiveModule);
const factory = mod.componentFactories.find((comp) =>
comp.componentType === ArticleLIveComponent
);
const component = this.container.createComponent(factory);
Object.assign(component.instance, properties);
}
}
As you can see, I can call the addHtmlComponent method to compile the new component at runtime using a custom HTML template.
My template looks like this:
<div>
<h2>Foo bar</h2>
<mat-tab-group>
<mat-tab label="Tab 1">Content 1</mat-tab>
<mat-tab label="Tab 2">Content 2</mat-tab>
</mat-tab-group>
<p>Other content</p>
, AOT (fyi use: https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack)
:
, , , AOT "" Angular .
- , .
- , , , Angular ( Angular ). . , AOT. - "" .
, Angular:
- : https://github.com/angular/material2/tree/master/src/material-examples
:
https://github.com/angular/material.angular.io/blob/master/src/app/shared/doc-viewer/doc-viewer.ts#L85
, , , .
EDIT: : https://github.com/yanis-git/aot-jit-angular ( )
, AOT , :
Module not found: Error: Can't resolve '@angular/compiler/src/config'
compilater Factory AppModule, .
, "lazy-jit", , , :
ERROR Error: No NgModule metadata found for 'ArticleLiveModule'.
, : https://github.com/angular/angular/issues/16033