遍历对象数组的 json 对象,信任一个键的值作为 Angular 中的 HTML
iterating over json object of arrays of objects, trusting one key's value as HTML in Angular
我在下面有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>
相关文章:
- 引用对象中的通用值
- 如何为json对象中的段发送array[]
- 对具有ui typeahead的对象中的值执行orderBy
- 使用同一对象中的其他变量声明变量
- 使用javascript比较对象中的对象值和数组长度
- 替换另一个变量对象中的变量值
- 对象中的str.replacement()
- 原始javascript将json对象中的项目打印到列表中的屏幕上
- 保存嵌入对象中的svg
- 求和数组对象中的数组值
- 如何使用for in循环在javascript中显示对象中的对象
- lodash:重命名对象中的键
- Don't将对象中的项附加到html中
- Javascript对象中的跨浏览器密钥查找性能
- AngularJS-筛选对象中的选定属性
- 将URL字符串${userid}替换为javascript对象中的实际用户id
- 是否可以在不重新渲染的情况下显示/隐藏父对象中的元素
- 如何通过json将products对象中的对象值从php传递给smarty
- 访问对象中的数组
- 将原型调用为新对象中的另一个原型