Strip html in Angular template binding

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

I have a list displaying data that can sometimes contain HTML

  
  • 2条回答
    •  长情又很酷
      2021-02-06 11:26

      I wouldn't recommend using a regex to parse html as suggested by kite.js.org. Use the browsers textContent / innerText function instead:

      htmlToText(html: string) {
          const tmp = document.createElement('DIV');
          tmp.innerHTML = html;
          return tmp.textContent || tmp.innerText || '';
      }
      

      This should be much more reliable. You can still use a pipe if you like, just don't use regex to parse html!

    提交回复
    热议问题