Angular2 Pipes: output raw HTML

后端 未结 9 2100
春和景丽
春和景丽 2020-12-13 17:43

I\'m trying to create an Angular2 custom pipe that outputs raw html. I want it to simply convert newlines in the input into HTML line breaks. How do I output raw HTML from a

9条回答
  •  没有蜡笔的小新
    2020-12-13 17:57

    As a variation of the above CSS answer by Toolkit, if you want whitespace to be counted rather than collapsed, you can use the following code instead.

    HTML

    
      {{text}}
    
    

    CSS

    .line-breaker {
      white-space: pre-wrap;
    }
    

    What pre-wrap does (in addition to showing line breaks and wrapping etc like pre-line) is show every space as a space, rather than collapsing multiple spaces into one.

    normal

    Sequences of whitespace are collapsed. Newline characters in the source are handled the same as other whitespace. Lines are broken as necessary to fill line boxes.

    nowrap

    Collapses whitespace as for normal, but suppresses line breaks (text wrapping) within the source.

    pre

    Sequences of whitespace are preserved. Lines are only broken at newline characters in the source and at
    elements.

    pre-wrap

    Sequences of whitespace are preserved. Lines are broken at newline characters, at
    , and as necessary to fill line boxes.

    pre-line

    Sequences of whitespace are collapsed. Lines are broken at newline characters, at
    , and as necessary to fill line boxes.

    Source: https://developer.mozilla.org/en/docs/Web/CSS/white-space

提交回复
热议问题