Toggle navigation
P3X GitList Snapshot
GitHub
Repo
Changelog
To do
Releases
Themes
Change log
Loading change log ...
To do ...
Loading todo ...
browsing:
a059c72a44e30588731bfddb7c6af165509e24a5
Branches
master
Tags
1.1.129-287
1.1.113-149
1.1.108-143
1.1.95-138
1.1.92-119
1.0.35-18
1.0.13-14
Files
Commits
Log
Graph
Stats
angular-compile.git
src
CompileService.ts
RSS
Git
Fetch origin
Download
ZIP
TAR
Clone
Raw
View
History
Clone
SSH
HTTPS
Blames found: 17
Mode: application/typescript
Binary: false
Hang on, we reloading big blames...
f46e4d96
import { Component, NgModule, Injectable, Compiler, ViewContainerRef, ModuleWithProviders, Type,
69db3407
Optional
f46e4d96
} from '@angular/core';
69db3407
import { CommonModule } from '@angular/common'; import { BrowserModule } from '@angular/platform-browser';
f46e4d96
export interface CompileOptions { template: string; container: ViewContainerRef; imports?: Array<Type<any> | ModuleWithProviders | any[]>; context?: any, onCompiled?: Function, onError?: Function;
69db3407
module?: NgModule;
f46e4d96
} const cache : any = {};
69db3407
export class CompileServiceConfig { module: NgModule } let SingletonDefaultModule: NgModule;
f46e4d96
@Injectable() export class CompileService {
69db3407
f46e4d96
constructor( private compiler: Compiler,
69db3407
@Optional() config: CompileServiceConfig,
f46e4d96
) {
69db3407
if (config !== undefined) { SingletonDefaultModule = config.module; }
f46e4d96
} public async compile(opts: CompileOptions) { try { const factory = await this.createFactory(opts); opts.container.clear(); const cmp : any = opts.container.createComponent(factory); cmp.instance.context = opts.context; } catch (e) { if (opts.onError) { opts.onError(e) } else { console.error(e); } } } private async createFactory(opts: CompileOptions) { const cacheKey = opts.template; if (Object.keys(cache).indexOf(cacheKey) > -1) { return cache[cacheKey]; } cache[cacheKey] = new Promise(async(resolve) => { @Component({ template: opts.template }) class TemplateComponent { context: any }
69db3407
let module : NgModule; if (opts.module !== undefined) { module = Object.assign({}, opts.module); } else { module = Object.assign({}, SingletonDefaultModule) || { // imports: opts.imports, }; } module.imports = module.imports || []; module.imports.push( CommonModule ); module.imports.push( BrowserModule ); if (opts.imports !== undefined) { module.imports = module.imports.concat(opts.imports) } if (module.declarations === undefined) { module.declarations = [ TemplateComponent ]; } else { module.declarations.push(TemplateComponent); } @NgModule(module)
f46e4d96
class TemplateModule { } const component = await this.compiler.compileModuleAndAllComponentsAsync(TemplateModule); const factory = component.componentFactories.find((comp) => comp.componentType === TemplateComponent ); if (opts.onCompiled) { opts.onCompiled(component); } cache[cacheKey] = factory; resolve(factory); }) return cache[cacheKey]; } }