59

AngularJS パーシャル内で、次のようにエントリのリストをループしています。

<ul class="entries">
    <li ng-repeat="entry in entries">
        <strong>{{ entry.title }}</strong>
        <p>{{ entry.content }}</p>
    </li>
</ul>

のコンテンツに{{entry.content}}は、AngularJS によって無視されるいくつかの改行があります。改行を保持するにはどうすればよいですか?

4

4 に答える 4

104

これは単なる基本的なHTMLです。AngularJSはそれについて何も変更しません。pre代わりにタグを使用できます。

<pre>{{ entry.content }}</pre>

またはCSSを使用します。

p .content {white-space: pre}

...

<p class='content'>{{ entry.content }}</p>

HTMLコードが含まれている場合entry.contentは、次を使用できますng-bind-html

<p ng-bind-html="entry.content"></p>

ngSanitizeを含めることを忘れないでください:

var myModule = angular.module('myModule', ['ngSanitize']);
于 2013-03-16T12:56:45.357 に答える
35

フィルターを作っています

// filters js
myApp.filter("nl2br", function($filter) {
 return function(data) {
   if (!data) return data;
   return data.replace(/\n\r?/g, '<br />');
 };
});

それから

// view
<div ng-bind-html="article.description | nl2br"></div>
于 2013-11-17T17:10:31.333 に答える
12
// AngularJS filter
angular.module('app').filter('newline', function($sce) {
    return function(text) {
        text = text.replace(/\n/g, '<br />');
        return $sce.trustAsHtml(text);
    }
});

// HTML
<span ng-bind-html="someText | newline"></span>
于 2015-06-30T16:46:24.443 に答える