Angular 2如何编译模板并保存结果html

Angular 2如何编译模板并保存结果html,angular,angular2-template,templating,Angular,Angular2 Template,Templating,好的,现在我必须为电子邮件创建一个正文 我需要获取一个模板,用我已有的数据(js对象)插入它,并将生成的html字符串放入一个变量中,这样我就可以将此正文发送到实际发送电子邮件的服务器 我不确定是否使用其他模板引擎(例如Handlebar)来生成此html 有没有办法使用angular 2模板引擎?请记住,我不需要展示它,我只想把它保存在内存中。 如果我不能使用它,我应该使用什么?有什么最著名或推荐的方法吗?不确定您为什么需要它,但这里有一些想法: import {Component, NgMo

好的,现在我必须为电子邮件创建一个正文

我需要获取一个模板,用我已有的数据(js对象)插入它,并将生成的html字符串放入一个变量中,这样我就可以将此正文发送到实际发送电子邮件的服务器

我不确定是否使用其他模板引擎(例如Handlebar)来生成此html

有没有办法使用angular 2模板引擎?请记住,我不需要展示它,我只想把它保存在内存中。


如果我不能使用它,我应该使用什么?有什么最著名或推荐的方法吗?

不确定您为什么需要它,但这里有一些想法:

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 {}
从'@angular/core'导入{Component,NgModule,Input,VERSION,ChangeDetectorRef}
从“@angular/platform browser”导入{BrowserModule}
从“@angular/core”导入{Injectable,ComponentFactoryResolver,ApplicationRef,Injector};
@组成部分({
选择器:'my-cmp',
模板:'这里是{{title}}'
})
导出类MyComponent{
@输入()标题:字符串;
}
@组成部分({
选择器:“我的应用程序”,
模板:`
创造
`,
})
导出类应用程序{
构造函数(专用componentFactoryResolver:componentFactoryResolver,
私有appRef:ApplicationRef,
专用注射器(注射器){
}
创建(标题){
让工厂=this.componentFactoryResolver.resolveComponentFactory(MyComponent);
设ref=factory.create(this.injector);
ref.instance.title=标题;
本附件附件视图(参考主机视图);
console.log(参考location.nativeElement);
此.appRef.detachView(参考主机视图);
}
}
@NGD模块({
导入:[BrowserModule],
声明:[应用程序,MyComponent],
EntryComponent:[MyComponent],
引导:[应用程序]
})
导出类AppModule{}