将新行/ n转换为角的换行符

Rk *_*iri 3 css whitespace linefeed angularjs angular-filters

我有一个包含换行符/ n的字符串。尝试显示

字符串。而不是将/ n作为新行,而是将“ / n”显示为文本。

   $scope.myOutput = " Hello /n"

    {{ myOutput | textFormat }}
Run Code Online (Sandbox Code Playgroud)

必需->您好(在html页上)

尝试过:

 app.filter('textFormat', function() {
    return function(x) {
      return x.replace(/\\n/g, '<br/>');
   }
Run Code Online (Sandbox Code Playgroud)

尝试过CSS样式,例如空白:pre;

小智 12

在 Angular 中,您可以通过输入以下内容轻松将文本转换为原始格式:

成分:

this.myText = 'This is line one\nThis is line 2\nAnd here is 3'
Run Code Online (Sandbox Code Playgroud)

html:

<div [innerText]='myText'></div>
Run Code Online (Sandbox Code Playgroud)


Tim*_*och 9

它不会替代它,但是您可以使用CSS属性white-space: pre-line;在浏览器中呈现\ n:https : //developer.mozilla.org/zh-CN/docs/Web/CSS/white-space

div {
  white-space: pre-line;
}
Run Code Online (Sandbox Code Playgroud)
<div>Foo
   Bar
       Baz     Foo
     Bar
 


     
  Baz
</div>
Run Code Online (Sandbox Code Playgroud)