Angular 5 adding html content dynamically

前端 未结 3 478
礼貌的吻别
礼貌的吻别 2020-12-06 04:38

I have following angular to add dynamically loaded content:

main.html

3条回答
  •  佛祖请我去吃肉
    2020-12-06 05:24

    The ComponentFactoryResolver's resolveComponentFactory method accepts an Angular Component.

    In your case, you are injecting HTML into your template, not a component. To inject HTML, save it in a variable and use the DomSanitizer to either sanitize it or 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);
        })
      };
    }
    

    Then, in your template:

提交回复
热议问题