首页 文章

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

提问于
浏览
0

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

I need to take a template, interpolate it with the data I already have (js Object) and have this resulted html string in a variable ,所以我可以将此正文发送到实际发送电子邮件的服务器 .

我不确定使用其他模板引擎,例如把手来生成这个html .

Is there a way to use the angular 2 template engine? Keep in mind that I dont need to show it, I want to keep it in memory only.

如果我不能使用它我应该使用什么?有没有最知名的或推荐的aprouch?

1 回答

  • 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 {}
    

相关问题