gpt4 book ai didi

javascript - 迭代对象数组的 json 对象,将一个键的值信任为 Angular 中的 HTML

转载 作者:行者123 更新时间:2023-11-28 07:49:19 24 4
gpt4 key购买 nike

我有下面的 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>

关于javascript - 迭代对象数组的 json 对象,将一个键的值信任为 Angular 中的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com