我从服务器上得到了如下数据:
{
"pageIndex": 0,
"pageSize": 30,
"recordCount": 0,
"records": [
{
"auditContent": "",
"auditID": 354,
"auditStatus": 3,
"bizStatus": 1,
"bodyPart": "2",
"categoryID": 141,
"city": "上海",
"desc": "22",
"duration": 2,
"forbidden": "2",
"indications": "2",
"name": "<div><span style='color:red;'>头部</span></div>按摩"
}
]
}我使用Angular.js来显示HTML tag.Code的结果,如下所示:
<td ng-bind-html='spuWebDTO.name'></td>但是,它不起作用,我发现视图结果丢弃了<div><span style='color:red;'></span></div>标记.Only显示的文本:头部按摩
为什么和如何解决这个问题。
发布于 2016-07-25 06:10:41
试试这个,
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope,$sce) {
$scope.obj = {
"pageIndex": 0,
"pageSize": 30,
"recordCount": 0,
"records": [
{
"auditContent": "",
"auditID": 354,
"auditStatus": 3,
"bizStatus": 1,
"bodyPart": "2",
"categoryID": 141,
"city": "上海",
"desc": "22",
"duration": 2,
"forbidden": "2",
"indications": "2",
"name": "<div><span style='color:red;'>头部</span></div>按摩"
}
]
}
$scope.content = $sce.trustAsHtml($scope.obj.records[0].name);
});<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link href="style.css" rel="stylesheet" />
<script data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js" data-require="angular.js@1.4.x"></script>
<script src="app.js"></script>
</head>
<body ng-app="plunker" ng-controller="MainCtrl">
<p ng-bind-html="content"></p>
</body>
</html>
发布于 2016-07-25 05:54:35
嗨,你需要把ngSanitize添加到你的应用模块中,
`angular.module('bindHtmlExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', function($scope) {
$scope.myHTML =
'I am an <code>HTML</code>string with ' +
'<a href="#">links!</a> and other <em>stuff</em>';
}]);`可以在模板中使用ng-bind-html。
<div ng-controller="ExampleController">
<p ng-bind-html="myHTML"></p>
</div>https://stackoverflow.com/questions/38560496
复制相似问题