我在 angularjs 数组中有一些 html 内容:
var HTMLs = [];
HTMLs
是一个字符串数组,其中包含以下 HTML 内容。
<p><img src="http: //www.pw.com/Emblems/598e97fa05454766902650b4c01d7645.jpg" style="width: 25%;" /><p>
<p><span style="font-weight: bold;">This is a sample heading.</span></p><p><span style="font-weight: bold; text-decoration: underline;">Some content without any image.</span></p>
我希望在为数组中的每个 html 内容在大小为 500x500 的 div 中呈现后显示这些 HTML 文本的预览图像。
我不想在 div
中为 HTML
呈现任何滚动条。如果 HTML
文本太长,那么小部分渲染对我来说就可以了。
最佳答案
可以通过getElementById方法从空洞字符串中取出需要的html部分,赋值给div。
var xSection = document.getElementById("sectionid");
document.getElementById("divid").innerHTML = xSection.innerHTML;
或者您可以在 HTML 中使用 ng-bind-html:
<div ng-bind-html="anyVariable"></div>
此时你会得到一个attempting to use an unsafe value in a safe context错误,所以你需要使用$sce来解决这个问题。
$sce.trustAsHtml()
在 Controller 中转换html字符串。
$scope.anyVariable = $sce.trustAsHtml(someHtmlVar);
关于html - 从 500x500 大小的 div 中的字符串呈现 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814230/