html - 从 500x500 大小的 div 中的字符串呈现 HTML

标签 html angularjs rendering

我在 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/

相关文章:

javascript - 如何自定义文本区域以使用户能够像 MS Word 列一样书写

Javascript - 创 build 置 div 宽度的动态类

javascript - 如何在html代码中使用json文件

css - UI Bootstrap 在 <uib-tabset> 指令周围添加了 <div>,破坏了 CSS

javascript - HTML 不渲染 JS 中给出的标签位置

javascript - 无法循环遍历 div 的子元素

javascript - 动态更改 ng-click 值

javascript - 如何滚动到谷歌地图容器 div?

python - 如何获得渲染的 Sprite 图像的连续移动(pygame)

javascript - 用户登录后 Meteor 重新渲染模板