AngularJS在引导表中将文本解析为HTML
AngularJS parse text as HTML in bootstrap table
我想在引导表中显示一些文本。文本应该被解析为HTML,因为我需要<br/>
-标签来显示彼此下面的所有不同链接。看到这个Plunker与我的代码。
正如您所看到的,我使用了默认的ng-bind
指令以及已弃用的ng-bind-html
指令,但是文本要么显示为纯文本,要么根本不显示。
你们能告诉我如何显示解析为HTML的文本吗?
<table class="table">
<thead>
<tr>
<th>Method</th>
<th>$scope.other</th>
<th>$scope.links</th>
</tr>
</thead>
<tbody>
<tr>
<td>ng-bind</td>
<td>{{other}}</td>
<td>{{links}}</td>
</tr>
<tr>
<td>ng-bind-html</td>
<td ng-bind-html="other"></td>
<td ng-bind-html="links"></td>
</tr>
</tbody>
</table>
匹配控制器:
app.controller('TestController', function($scope){
$scope.links = 'https://www.google.com <br/> https://angularjs.org/ <br/> '
https://www.google.com <br/> https://angularjs.org/ <br/> '
https://www.google.com <br/> https://angularjs.org/ <br/>';
$scope.other = "sample Text";
});
默认情况下注入到模板中的HTML是不安全的,你必须将其标记为受信任。
app.controller('TestController', function($scope, $sce){
$scope.links = $sce.trustAsHtml('https://www.google.com <br/> https://angularjs.org/ <br/> '
https://www.google.com <br/> https://angularjs.org/ <br/> '
https://www.google.com <br/> https://angularjs.org/ <br/>');
$scope.other = "sample Text";
});
是否有你需要直接注入HTML的原因?你可以有一个地图/链接列表,并使用ng-repeat
在你的模板中呈现它们。
您需要使用消毒模块:https://docs.angularjs.org/api/ngSanitize/service/$sanitize
app.controller('TestController', function($scope, $sce){
$scope.links = $sce.trustAsHtml('https://www.google.com <br/> https://angularjs.org/ <br/> '
https://www.google.com <br/> https://angularjs.org/ <br/> '
https://www.google.com <br/> https://angularjs.org/ <br/>');
$scope.other = "sample Text";
});
您可以使用ng-bind-html
和ng-bind-html-unsafe
来实现这种目的。
示例如下
相关文章:
- 我收到消息“资源解释为脚本,但使用 MIME 类型文本/html 传输”
- 如何使按钮单击并更改以前显示的文本(html)
- 使用 Javascript 解析电子邮件源的文本/html 部分
- Javascript/jQuery,将CSS应用于包含特定文本/html的元素
- jQuery文本html操作,以查找大量文本中出现的字符,然后更改其颜色
- 使用 Jquery 创建具有转义文本 + HTML 的元素
- 在空节点(如文本节点)中添加文本/html
- 从网站获取特定文本 (html)
- 如何解决“资源解释为脚本但使用 MIME 类型文本/html 传输”
- 请求 JSONP 时的文本/html 输出
- 将元素添加到 DOM 给定的纯文本 HTML 中,仅使用纯 JavaScript(没有 jQuery)
- 在gmail撰写邮件区域中获取选中/突出显示的文本html
- 如何确定是否在父元素的开始或结束存在文本?HTML DOM &JavaScript
- 在输入文本html中显示格式
- 如何使纯文本html javascript
- 资源解释为脚本,但传输MIME类型文本/html javascript
- 从Javascript传递文本- HTML- PHP
- 从DOM对象中获取文本/html
- 脚本类型=“文本/ html"从文件中包含HTML
- jsFiddler.NET中的文本/HTML编辑器