如何在html中格式化存储的数据

时间:2017-01-17 06:26:53

标签: javascript html angularjs

我有如下数据,我想根据模板中的格式显示数据。 我的数据,

[ 

{
    "_id" : ObjectId("587daf9604a192098ac39fe1"),
    "uuid" : "3a06d55e-b2b0-4e24-9c17-fdf96a3827e6",
    "active" : true,
    "created" : ISODate("2017-01-17T05:45:58.919Z"),
    "description" : "khsjq<br />wsjqwsqw<br />wwsnqwksqwsws"
}, 
{
    "_id" : ObjectId("587db2f404a192098ac39fe3"),
    "uuid" : "454a1ff2-d64d-48d6-bb01-b3bc814aca01",
    "active" : true,
    "created" : ISODate("2017-01-17T06:00:20.439Z"),
    "description" : "iifdjasda<br />dlsldsad<br />asd<br />sadsadasdasdsadsadsad"
}]

我的js

   $.each(vm.courseObj.course_module, function (i, v) {
          v.description = $sce.trustAsHtml(v.description);
          vm.courseModulearr.push(v);
      });

不幸的是,它没有反映在我看来,它显示为 iifdjasda
dlsldsad
asd
sadsadasdasdsadsadsad在我看来。任何人都可以帮助我。谢谢。

2 个答案:

答案 0 :(得分:1)

像这样创建一个trust过滤器

.filter('trust', [
    '$sce',
    function($sce) {
      return function(value, type) {
        return $sce.trustAs(type || 'html', value);
      }
    }
  ]);

并像这样绑定值

<div ng-bind-html="htmlData | trust"></div>

结帐我的工作fiddle

答案 1 :(得分:1)

你可以看看我的plunkr ..

https://plnkr.co/edit/EVPDSFAUyr55jDDlVOVV?p=preview

  app.filter("unsafe",function($sce){

        return function(val){
          return $sce.trustAsHtml(val);
        }
      })

您应该使用过滤器和ng-bind-html来解析html内容