json - 解析 JSON 以角度显示数据
问题描述
因此,我将数据存储在数据库中,并且以角度提取数据,并且正在获取数据问题是当我尝试显示数据时出现以下错误。我只需要使用ngFor * 在 html 中显示问题。我对 Angular 完全陌生,任何指导都会非常有帮助。
this.selfHelpService.getFgSelfHelpByName("BYOD").subscribe(data => {
// console.log(data);
const BYOD = JSON.parse(data["data"]);
// console.log(BYOD );
});
<div class="cards" *ngFor="let BYOD of BYOD">
<div class="read-1">
{{BYOD.posts.question}}
</div>
</div>
解决方案
由于您正在获取数据,因此您可以执行以下操作:
// If this is your response:
{"BYOD":[ ` { "posts": [ {"Question":"BYOD (Android)","answer":"???"}, {"Question":"BYOD (IOS)","answer":"?????"} ] } ] };
this.selfHelpService.getFgSelfHelpByName("BYOD").subscribe(data => {
try {
const BYOD = JSON.parse(JSON.stringify(data["data"]));
} catch(err) {
console.log('Try catch error ==>', err);
}
// Declare a variable in the component.ts above constructor called byodPostsData
// Assign the values to that variable
this.byodPostsData = BYOD[0].posts;
});
// In your HTML now, simply loop through the this.byodPostsData;
<div class="cards" *ngFor="let eachPost of byodPostsData">
<div class="read-1" *ngIf="eachPost && eachPost.question">
{{ eachPost.question }}
</div>
</div>
推荐阅读
- node.js - 如何使用 mongoose 根据 mongoDb 中的特定时间段查询数据
- svelte - 如何为 staging env 构建 svelte-kit?
- jquery - d3在调用函数上强制图更新
- javascript - 为什么firebase在部署时给我一个错误?
- javascript - 如何使用 ejs 从 javacsript 中的 node.js 访问数组
- javascript - Javascript 使用 React js 获取 H:M:S 中的日期格式
- c# - 当我尝试让它向上/向下悬浮时,我的模型不会保持不变
- matlab - 筛选算法的 RMSE
- python - 管理多个 Python 安装
- ns-3 - 如何在 ns3 中的位置上修复节点位置