首页 > 解决方案 > 解析 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>

在此处输入图像描述

标签: jsonangular

解决方案


由于您正在获取数据,因此您可以执行以下操作:


// 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>


推荐阅读