Strip html in Angular template binding

后端 未结 2 1136
梦如初夏
梦如初夏 2021-02-06 10:51

I have a list displaying data that can sometimes contain HTML

  
  • 2条回答
    •  Happy的楠姐
      2021-02-06 11:14

      I guess there is no direct way to strip HTML tags from string, you can use Pipe, write a "Pipe" like this:

      import { Pipe, PipeTransform } from '@angular/core';
      
      @Pipe({
          name: 'striphtml'
      })
      
      export class StripHtmlPipe implements PipeTransform {
          transform(value: string): any {
              return value.replace(/<.*?>/g, ''); // replace tags
          }
      }
      

      then add "StripHtmlPipe" to your module "declarations", after these steps you can use this pipe in your HTML:

    • {{result.question.title | striphtml}}
    • please note that the code above is not fully tested.

    提交回复
    热议问题