在 Ionic 中显示预先格式化的 HTML 内容
Displaying pre-formatted HTML content in Ionic
我已经预先格式化了 HTML 需要在 Ionic 中显示的内容(来自第三方)。例如,我有从 DB 获得的问题列表(预格式化 html),我需要在 Ionic 中显示为 Ion-Items 或 Ion-Card 列表。
示例 HTML 来自数据库的内容:
第一条记录:
<p>
<span style="color: #eb4924;"><strong>SOME_TEXT_HEADER 1</strong></span><br />
TEXT_DESCRIPTION 1<br />
<span style="color: #339966;"><strong>SOME_COMMENTS_HEADER:</strong></span><br />
SOME_COMMENT_TEXT_1<br />
SOME_COMMENT_TEXT_2<br />
<img src="https://xyx.com/SOME_IMAGE.jpg" alt="ALT_TEXT" width="320" height="116"/>
SOME_COMMENT_TEXT_3<br />
</p>
第二条记录
<p>
<span style="color: #eb4924;"><strong>SOME_TEXT_HEADER 2</strong></span><br />
TEXT_DESCRIPTION 2<br />
<span style="color: #339966;"><strong>SOME_COMMENTS_HEADER:</strong></span><br />
SOME_COMMENT_TEXT_1<br />
SOME_COMMENT_TEXT_2<br />
<img src="https://xyx.com/SOME_IMAGE.jpg" alt="ALT_TEXT" width="320" height="116"/>
SOME_COMMENT_TEXT_3<br />
</p>
第三条记录:
<p>
<span style="color: #eb4924;"><strong>SOME_TEXT_HEADER 3</strong></span><br />
TEXT_DESCRIPTION 3<br />
<span style="color: #339966;"><strong>SOME_COMMENTS_HEADER:</strong></span><br />
SOME_COMMENT_TEXT_1<br />
SOME_COMMENT_TEXT_2<br />
<img src="https://xyx.com/SOME_IMAGE.jpg" alt="ALT_TEXT" width="320" height="116"/>
SOME_COMMENT_TEXT_3<br />
</p>
...等等
我想获得有关处理这种情况的最佳做法的建议。任何帮助将不胜感激。谢谢!
您可以使用 Angular 中的 DomSanitizer 来显示您的 html。
Html :
<div class="details" [innerHtml]="details">
</div>
</ion-content>
在你的 ts 中:
displayHTML(data){
this.details = this.sanitizer.bypassSecurityTrustHtml(data);
});
不要忘记在构造函数中声明:
public sanitizer: DomSanitizer
我已经预先格式化了 HTML 需要在 Ionic 中显示的内容(来自第三方)。例如,我有从 DB 获得的问题列表(预格式化 html),我需要在 Ionic 中显示为 Ion-Items 或 Ion-Card 列表。
示例 HTML 来自数据库的内容:
第一条记录:
<p>
<span style="color: #eb4924;"><strong>SOME_TEXT_HEADER 1</strong></span><br />
TEXT_DESCRIPTION 1<br />
<span style="color: #339966;"><strong>SOME_COMMENTS_HEADER:</strong></span><br />
SOME_COMMENT_TEXT_1<br />
SOME_COMMENT_TEXT_2<br />
<img src="https://xyx.com/SOME_IMAGE.jpg" alt="ALT_TEXT" width="320" height="116"/>
SOME_COMMENT_TEXT_3<br />
</p>
第二条记录
<p>
<span style="color: #eb4924;"><strong>SOME_TEXT_HEADER 2</strong></span><br />
TEXT_DESCRIPTION 2<br />
<span style="color: #339966;"><strong>SOME_COMMENTS_HEADER:</strong></span><br />
SOME_COMMENT_TEXT_1<br />
SOME_COMMENT_TEXT_2<br />
<img src="https://xyx.com/SOME_IMAGE.jpg" alt="ALT_TEXT" width="320" height="116"/>
SOME_COMMENT_TEXT_3<br />
</p>
第三条记录:
<p>
<span style="color: #eb4924;"><strong>SOME_TEXT_HEADER 3</strong></span><br />
TEXT_DESCRIPTION 3<br />
<span style="color: #339966;"><strong>SOME_COMMENTS_HEADER:</strong></span><br />
SOME_COMMENT_TEXT_1<br />
SOME_COMMENT_TEXT_2<br />
<img src="https://xyx.com/SOME_IMAGE.jpg" alt="ALT_TEXT" width="320" height="116"/>
SOME_COMMENT_TEXT_3<br />
</p>
...等等
我想获得有关处理这种情况的最佳做法的建议。任何帮助将不胜感激。谢谢!
您可以使用 Angular 中的 DomSanitizer 来显示您的 html。
Html :
<div class="details" [innerHtml]="details">
</div>
</ion-content>
在你的 ts 中:
displayHTML(data){
this.details = this.sanitizer.bypassSecurityTrustHtml(data);
});
不要忘记在构造函数中声明:
public sanitizer: DomSanitizer