遍历对象数组的 json 对象,信任一个键的值作为 Angular 中的 HTML

iterating over json object of arrays of objects, trusting one key's value as HTML in Angular

本文关键字:对象 中的 HTML 一个 Angular json 数组 信任 遍历      更新时间:2023-09-26

我在下面有JSON模式,我正在尝试迭代每个对象的描述并将其信任为HTML,以便我可以在应用程序中显示链接:

{
  "notes": {
    "long random ID number 1": [
     {
        "name": "test 1",
        "description": "<a href='http://google.com'>google</a>"
     }
    ],
    "long random ID number 2": [
      //more objects with names and descriptions
    ]
  }
}

在笔记中,有许多JS对象数组,键是一个长而随机的ID。 如何迭代每个对象数组而不考虑键,并在链接上运行 Angular 的$sce.trustAsHtml(),使其显示在 UI 中?

编辑:

var notesKeys = Object.keys($scope.requirements.notes);
for(var key in notesKeys)
{
    for(var note in $scope.requirements.notes[key])
    {
        $sce.trustAsHtml(note.description);
        $sce.trustAsHtml(note.name);
        console.log(note.description);
        console.log(note.name);
    }
}
我想

这样的东西可以解决问题:

$scope.links = [];
//Get the keys of your hash, object is your complete object
var notesKey = Object.keys(object.notes)
//iterates over keys in notes
for(var i=0; i < notesKey.length; i++){
  // iterates over each object
  var notes = object.notes[notesKey[i]];
  for (var j=0; j < notes.length ; j++){
   //You have your object so do what you like
   $scope.links.push($sce.trustAsHtml(notes[j].description));
  }
}

在你的html中使用ng-repeat和ng-bind-html,这取决于你想要什么:

<div ng-repeat="link in links" ng-bind-html="link"></div>