【问题标题】:Angular 5 adding html content dynamicallyAngular 5动态添加html内容
【发布时间】:2018-02-21 18:50:38
【问题描述】:

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

ma​​in.html

<div class="top">
   <ng-template #main></ng-template>
</div>

ma​​in.ts

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 标记,例如:

 <div class="sample"> Example </div>

但是我收到一个错误"Error: No component factory found for &lt;div class="sample"&gt; Example &lt;/div&gt;"

任何建议将不胜感激。

【问题讨论】:

    标签: angular


    【解决方案1】:

    ComponentFactoryResolver 的 resolveComponentFactory 方法接受 Angular 组件。

    在您的情况下,您将 HTML 注入到您的模板中,而不是组件中。要注入 HTML,请将其保存在变量中并使用DomSanitizer 对其进行清理或bypass the security check:

    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);
        })
      };
    }
    

    然后,在您的模板中:

    <div class="top">
      <div [innerHtml]="data"></div>
    </div>
    

    【讨论】:

    • 清理应该使用this.sanitizer.sanitize(SecurityContext.HTML, res)
    • 你知道吗,我们如何动态注入 html(有绑定)。绑定的值如何表示。目的是使绑定与组件中的属性一起工作。
    • @Darey 有什么解决办法吗?
    • 我认为这种方法不适用于角度组件或指令。例如:&lt;my-component&gt;&lt;/..&gt; 或 &lt;div [myDirective]="something"&gt;
    • @Saad 但根据我的阅读,为了使绑定起作用,我需要声明输入和输出。但我不认识他们。我的场景是我有一个用户创建的项目列表,它可以具有用户想要添加到它们的任何属性,当一个被选中时,我需要在右侧显示详细信息。详细信息来自后端的 html,使用绑定和所有内容构建,并且在另一个调用中需要绑定的数据。你有什么建议吗?
    【解决方案2】:

    有一个新的库可以完成这项工作。它被称为ngx-dynamic-hooks。链接here。

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

    【讨论】:

      【解决方案3】:
                   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');
              }
            });
      

      【讨论】:

      • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的回答添加解释并说明适用的限制和假设。
      猜你喜欢
      • 2019-02-18
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 2014-09-13
      • 2012-02-27
      • 2019-04-09
      相关资源
      最近更新 更多