首页 > 解决方案 > 在 Angular 5 中替换 HTML 电子邮件模板中的内容的最佳方法是什么?

问题描述

我有一个 HTML 电子邮件模板存储在assets/templates/commands-report.html

这个模板文件有*ngIF*ngFor [ngStyle]

在我的服务中,我编写了以下函数,该函数获取通过 HTTP 调用获得的模板文件内容。这在开发模式下完美运行。这意味着,我在 HTML 电子邮件模板中的所有数据都会被渲染,并且我能够将 HTML 作为 API 有效负载发送。但是,当我使用 进行生产构建ng build --prod并尝试导航到代码时,出现错误Runtime compiler is not loaded

public enrichHTMLEmailTemplate(template: string, vc: ViewContainerRef, data: any): Observable<any> {

let templateSubject = new Subject<any>();

const tmpCmp = Component({ template })(class {

  ngOnInit() {
  }
});

const tmpModule = NgModule({
  declarations: [tmpCmp],
  imports: [CommonModule]
})(class { });

let cmpRef: ComponentRef<any>;
this.compiler.compileModuleAndAllComponentsAsync(tmpModule)
  .then((factories) => {
    const f = factories.componentFactories[0];
    cmpRef = f.create(this.injector, [], null, this.moduleRef);
    cmpRef.instance.name = 'email-component';
    cmpRef.instance.data = data;
    vc.insert(cmpRef.hostView, 1);

    setTimeout(() => {

      templateSubject.next(cmpRef.location.nativeElement.innerHTML);
    }, 3000);
  });

return templateSubject.asObservable();
}

我正在使用角度 v5.2.0

让我知道从角度方面丰富 HTML 数据的好方法是什么。如果存在做同样的事情,也请指向我的任何 npm 包。

提前致谢。

标签: angularaot

解决方案


推荐阅读