首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角5动态添加html内容

角5动态添加html内容
EN

Stack Overflow用户
提问于 2018-02-21 18:50:38
回答 3查看 58.1K关注 0票数 23

我有以下角度来添加动态加载的内容:

main.html

代码语言:javascript
复制
<div class="top">
   <ng-template #main></ng-template>
</div>

main.ts

代码语言:javascript
复制
import { Component, ViewChild, ComponentFactoryResolver, ViewContainerRef  } from '@angular/core';

@Component({
    selector: 'page-main_page',
    templateUrl: 'main_page.html'
})
export class main_page {        
    @ViewChild('main', { read: ViewContainerRef }) entry: ViewContainerRef;
    data: any;

constructor(public resolver: ComponentFactoryResolver){ 

};      

    ngOnInit(){ 
        this.getDynamicREST().then((res)=>{
            this.data = res; //Where it is a html markup from php server: <div class="sample"> Example </div>

            const factory = this.resolver.resolveComponentFactory(this.data);
            this.entry.createComponent(factory);

        })
    };

}

getDynamicRest()中,我从php服务器获得一个html标记,如:

代码语言:javascript
复制
 <div class="sample"> Example </div>

但是,我得到了一个错误"Error: No component factory found for <div class="sample"> Example </div>"

如有任何建议,将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-21 19:50:50

ComponentFactoryResolverresolveComponentFactory方法接受一个角分量。

在您的示例中,您将HTML注入到模板中,而不是组件中。若要注入HTML,请将其保存在变量中,并使用DomSanitizer对其进行消毒或使用绕过安全检查

代码语言:javascript
复制
export class main_page {
  data: SafeHtml;

  constructor(private sanitizer: DomSanitizer) {}      

  ngOnInit(){ 
    this.getDynamicREST().then((res)=> {
        this.data = this.sanitizer.sanitize(res);
        /* OR */
        this.data = this.sanitizer.bypassSecurityTrustHtml(res);
    })
  };
}

然后,在模板中:

代码语言:javascript
复制
<div class="top">
  <div [innerHtml]="data"></div>
</div>
票数 52
EN

Stack Overflow用户

发布于 2020-10-08 11:06:54

有一个新的图书馆来完成这项工作。它被称为ngx-dynamic-hooks。链接这里

有了这个,您就不必禁用AOT (与ngx-dynamic-template一样,链接这里)。

票数 2
EN

Stack Overflow用户

发布于 2020-04-23 12:24:54

代码语言:javascript
复制
             let transporter = nodemailer.createTransport({
      host :'smtp.gmail.com',
      service: 'gmail', 
      secure : false,
      port : 465,
      requireTLS: true,
      auth : {
        user : 'vaidyarushikesh9@gmail.com',
        pass : 'PAssword', 
      }
    });     


             var email = req.body.email`enter code here`;
            let htmlContent = `<h1><strong>Foreget Password Link Form</strong></h1>
            <p>Hi,</p>
   <p>http://localhost:4200/forget/${email} contacted with the following Details</p>`;

      let mailoptions = {
      from : 'vaidyarushikesh9@gmail.com',
      to : req.body.email,
      subject : 'tesst',
      text: '',
      html: htmlContent
    };
      transporter.sendMail(mailoptions,function(err,data){
        if(err){
          console.log('errr',err)
        }else{
          console.log('email send');
        }
      });
票数 -5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48913344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档