我有一个带有使用 trNgGrid.js 的搜索选项的表。我对 Sharepoint 列表进行查询,其中一列具有 html 内容,我想显示它。
为此,我在 promise 中使用 $sce.trustAsHtml
,但这不起作用。因此,经过一些测试后,我注意到在执行 $scope.requests.push(item)
之后,我可以执行以下操作:
$scope.requests[x].SubjectText = $sce.trustAsHtml($scope.requests[x].HTMLContent);
这或多或少是有效的,因为SubjectText列不是用来显示html的,所以它的作用是将其显示为纯文本。
另一方面,如果我这样做,什么也不会发生:
$scope.requests[x].HTMLContent = $sce.trustAsHtml(angular.copy($scope.requests[x].HTMLContent));
该列没有数据。最奇怪的是,如果我做完全相同的事情,但没有告诉哪个项目我把它的值放在了工作中,那么html内容就会以完美的方式显示,但所有项目都具有相同的值,即最后一个项目的值 promise 。
$scope.requests.HTMLContent = $sce.trustAsHtml(angular.copy($scope.requests[x].HTMLContent));
我对 trNgGrid 进行了更改,这有助于我在有文件名的情况下在 div 中拥有 ng-bind-html
。
最佳答案
另一种选择是创建一个自定义列,您可以在其中使用 ng-bind-html
指令:
angular
.module('myApp', ['trNgGrid'])
.controller("MainCtrl", ["$scope", "$sce", function ($scope, $sce) {
$scope.myItems = [
{
name: "Moroni",
age: 50,
htmlContent: $sce.trustAsHtml('<i><b>HTML Content #1</b></i>')
},
{
name: "Tiancum",
age: 43,
htmlContent: $sce.trustAsHtml('<div class="text-danger">HTML Content #2</div>')
},
{
name: "Jacob",
age: 27,
htmlContent: $sce.trustAsHtml('<div class="text-info">HTML Content #3</div>')
},
{
name: "Nephi",
age: 29,
htmlContent: $sce.trustAsHtml('<div class="text-warning">HTML Content #4</div>')
},
{
name: "Enos",
age: 99,
htmlContent: $sce.trustAsHtml('<pre>HTML Content #5</pre>')
}];
}]);
<link href="//maxcdn.bootstrapcdn.com/bootswatch/3.0.3/slate/bootstrap.min.css" rel="stylesheet"/>
<link href="//moonstorm.github.io/trNgGrid/release/trNgGrid.min.css" rel="stylesheet"/>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.1/angular.min.js"></script>
<script src="//moonstorm.github.io/trNgGrid/release/trNgGrid.min.js"></script>
<body ng-app="myApp">
<div ng-controller="MainCtrl">
<table tr-ng-grid items="myItems" fields="['name','age']">
<thead>
<tr>
<th><div></div></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div ng-bind-html="gridItem.htmlContent"></div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
如果您不将 HTML 与数据分开(我总是建议这样做),那么您需要 fields
属性来阻止网格自动选取 htmlContent
数据成员。
关于javascript - 如何使用 $sce.trustAsHtml 在 trNgGrid 中显示 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410239/