如何在angularjs中实现html格式的字符串

How to achieve html formatted strings in angularjs

本文关键字:html 格式 字符串 实现 angularjs      更新时间:2023-09-26

我正在使用angularjs。这里我有一个从数据库中提取的字符串,它是像这样的html形式

$scope.data = '<p>this is first line </br> this is second line</p>';

当我像一样冷静下来的时候

{{$scope.data}}

它实际上没有像那样格式化的html

<p>this is first line </br> this is second line</p>

但我想成为

this is first line
this is second line

使用$sce:

$scope.data = $sce.trustAsHtml('<p>this is first line </br> this is second line</p>');

HTML

<div ng-bind-html="data"></div>

您可以使用Angular的$compile工具,您可以在这里找到文档。