Insert HTML into view from AngularJS controller

前端 未结 18 2107
Happy的楠姐
Happy的楠姐 2020-11-21 06:00

Is it possible to create an HTML fragment in an AngularJS controller and have this HTML shown in the view?

This comes from a requirement to turn an

18条回答
  •  遥遥无期
    2020-11-21 06:37

    Working example with pipe to display html in template with Angular 4.

    1.Crated Pipe escape-html.pipe.ts

    `

    import { Pipe, PipeTransform } from '@angular/core';
    import { DomSanitizer } from '@angular/platform-browser';
    @Pipe({name : 'keepHtml', pure : false})
    export class EscapeHtmlPipe implements PipeTransform{
     constructor(private sanitizer : DomSanitizer){
     }
     transform(content){
      return this.sanitizer.bypassSecurityTrustHtml(content);
     }
    }
    

    ` 2. Register pipe to app.module.ts

     import {EscapeHtmlPipe} from './components/pipes/escape-html.pipe';
        declarations: [...,EscapeHtmlPipe]
    
    1. Use in your template

          

    2. getDivHtml() { //can return html as per requirement}

      Please add appropriate implementation for getDivHtml in associated component.ts file.

提交回复
热议问题