Javascript 为什么ng bind html会丢弃我添加的html标记

Javascript 为什么ng bind html会丢弃我添加的html标记,javascript,angularjs,Javascript,Angularjs,我从服务器获取一些数据,如下所示: { "pageIndex": 0, "pageSize": 30, "recordCount": 0, "records": [ { "auditContent": "", "auditID": 354, "auditStatus": 3, "bizStatus": 1, "bodyPart": "2", "categoryID": 141, "city": "上海", "desc": "22", "duratio

我从服务器获取一些数据,如下所示:

{
 "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>按摩"
}
]
}
 <td ng-bind-html='spuWebDTO.name'></td>
{
“页面索引”:0,
“页面大小”:30,
“记录计数”:0,
“记录”:[
{
“审核内容”:“,
“海选”:354,
“审计状态”:3,
“bizStatus”:1,
“身体部位”:“2”,
“类别”:141,
“城市”:上海",
“描述”:“22”,
“期限”:2,
“禁止”:“2”,
“指示”:“2”,
“名称”:头部按摩"
}
]
}
我使用Angular.js用HTML标记显示结果。代码如下:

{
 "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>按摩"
}
]
}
 <td ng-bind-html='spuWebDTO.name'></td>

但是,它不起作用。我发现查看结果丢弃了HTML标记
。只显示文本:
头部按摩


为什么以及如何解决此问题。

您好,您需要将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>

试试这个,
var-app=angular.module('plunker',[]);
应用程序控制器('MainCtrl',函数($scope,$sce){
$scope.obj={
“页面索引”:0,
“页面大小”:30,
“记录计数”:0,
“记录”:[
{
“审核内容”:“,
“海选”:354,
“审计状态”:3,
“bizStatus”:1,
“身体部位”:“2”,
“类别”:141,
“城市”:上海",
“描述”:“22”,
“期限”:2,
“禁止”:“2”,
“指示”:“2”,
“名称”:头部按摩"
}
] 
}
$scope.content=$sce.trustAsHtml($scope.obj.records[0].name);
});

安古拉斯普朗克
文件。写(“”);


是否要按原样显示名称?我的意思是使用html标记和所有这些标记?如果是这样,您可以使用ng-bind。是否有可用的
$sanitize
检查此链接:我想显示html。在我的代码中,我想
头部以读取颜色显示。我已经添加了ngSanitize,比如
var-app=angular.module('Index',['ngSanitize']);
。我使用的方式有错误吗?我已经添加了这个模块。代码是
var-app=angular.module('Index',['ngSanitize']);app controller('IndexController',function($scope,$http)..…
但是查看结果不显示
。在我的代码中,我想要
头部以读取颜色显示。在控制器中注入$sce提供程序,如下示例
controller('AppController',['$http','$templateCache','$sce',函数($http,$templateCache,$sce){var self=this;self.explicitlyTrustedHtml=$sce.trustAsHtml('Hover over this text.');}]);
check“请给我看一个使用SCE的示例“示例I根据您的建议编写,与您的类似。但它不起作用。代码如下:

它在这里起作用。检查代码。您是在代码中编写的吗?$scope.content=$SCE.trustAsHtml($scope.obj.records[0].name);我找到了原因。服务器响应在HTML标记中添加了一些转义字符,因此无法工作。我使用了`spuWebDTO.name.replace(//\xA0/g,“”).replace(//g,“>”).replace(//g“,等等。不需要使用
$sce
,我的模块定义是
var-app=angular.module('Index',['ngSanitize');