2017-04-26 100 views
0

好吧,現在我必須創建一個電子郵件正文。Angular 2如何編譯模板並保存結果html

我需要一個模板,與我已經有(JS對象)中的數據進行插值,並擁有這導致HTML字符串變量,所以我這身發送到將實際發送電子服務器-郵件。

我不確定使用其他模板引擎,例如把手來生成這個html。

有沒有辦法使用角2模板引擎?請記住,我不需要顯示它,我只想保留在內存中。

如果我不能使用它,我應該使用什麼?有沒有最有名的或推薦的aprouch?

回答

0

不知道爲什麼你需要它,但這裏是一些建議:

import {Component, NgModule, Input, VERSION, ChangeDetectorRef} from '@angular/core' 
import {BrowserModule} from '@angular/platform-browser' 
import {Injectable, ComponentFactoryResolver, ApplicationRef, Injector} from '@angular/core'; 

@Component({ 
    selector:'my-cmp', 
    template: 'here is {{title}}' 
}) 
export class MyComponent { 
    @Input() title: string; 
} 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <input #inp> 
    <button (click)="create(inp.value)">Create</button> 
    `, 
}) 
export class App { 
    constructor(private componentFactoryResolver: ComponentFactoryResolver, 
       private appRef: ApplicationRef, 
       private injector: Injector) { 
    } 

    create(title) { 
    let factory = this.componentFactoryResolver.resolveComponentFactory(MyComponent); 
    let ref = factory.create(this.injector); 
    ref.instance.title = title; 
    this.appRef.attachView(ref.hostView); 

    console.log(ref.location.nativeElement); 

    this.appRef.detachView(ref.hostView); 
    } 
} 

@NgModule({ 
    imports: [ BrowserModule ], 
    declarations: [ App , MyComponent], 
    entryComponents: [MyComponent], 
    bootstrap: [ App ] 
}) 
export class AppModule {}