在Ionic中渲染HTML内容

Apa*_*dos 6 angularjs ionic-framework ionic

我有以下模板:

<ion-view view-title="Playlist">
Run Code Online (Sandbox Code Playgroud)

  <div class="list list-inset">
      <div class="item item-divider item-text-wrap">
          {{post.titulo}}
      </div>
      <div class="item">
          <img src="{{post.image}}" width="100%" />
      </div>
      <div class="item item-divider" style="font-size:14px;font-weight:normal;text-align:right">
          {{post.fecha}} - By: {{post.autor}}
      </div>
      <div class="item item-text-wrap">
          {{ post.contenido }}
      </div>
  </div>
Run Code Online (Sandbox Code Playgroud)

问题是' post.contenido '有我希望显示的HTML,但它只显示HTML标签和文本.

我该怎么做才能呈现这样的内容?

亲切的问候.

Dua*_*nnx 5

我认为您应该将代码更改为:
<div class="item item-text-wrap" [innerHTML]="post.contenido"> </div>


S.G*_*eau 4

假设您有一个包含 html 的作用域变量!

$scope.someHTML = "<h1>Big Nice Title here</h1>";
Run Code Online (Sandbox Code Playgroud)

你应该能够像这样输出它

<div ng-bind-html-unsafe="someHTML"></div>
Run Code Online (Sandbox Code Playgroud)

..在你的情况下应该是这样的

[...]
<div class="item item-text-wrap" ng-bind-html-unsafe="post.contenido"></div>
[...]
Run Code Online (Sandbox Code Playgroud)

  • 已使用指令“ng-bind-html”而不是“ng-bind-html-unsafe”解决了这个问题。 (2认同)