简体   繁体   English

如何使用 Angular 获取典型的 json 数据

[英]How to fetch typical json data using Angular

 import { Component, OnInit } from '@angular/core'; import { ResumedataService } from "../../services/resumedata.service"; @Component({ selector: 'app-projects', templateUrl: './projects.component.html', styleUrls: ['./projects.component.scss'] }) export class ProjectsComponent implements OnInit { projectdata: any = []; constructor(private resumeservice: ResumedataService) {} ngOnInit() { // service call this.resumeservice.getresumedata().subscribe(data => (this.projectdata = data.projects)); } }
 <div *ngFor="let item of projectdata"> <h5>{{item.projectName}}</h5> <span>{{item.technologies.tech}}</span> </div> //******this is jason data******* "projects": [ { "id":1, "projectName": "Flight Reservation System", "technologies": [ {"tech":"html"}, {"tech":"css"}, {"tech":"javascript"}, {"tech":"jquery"}, {"tech":"sql"}, {"tech":"angular"} ] }, { "id":2, "projectName": "Train Reservation System", "technologies": [ {"tech":"html"}, {"tech":"css"}, {"tech":"javascript"}, {"tech":"jquery"}, {"tech":"sql"}, {"tech":"angular"} ] } ]

Above is my source code as you can see i want to fetch data through as I am successfully getting the name of the project but the object called "technologies" has children so how can I fetch them take a look if there is any mistake..??上面是我的源代码,你可以看到我想通过获取数据,因为我成功地获得了项目的名称,但名为“技术”的对象有孩子,所以如果有任何错误,我如何获取他们看看.. ??

Just use another *ngFor :只需使用另一个*ngFor

<div *ngFor="let item of data">
  {{item.projectName}}
  <br />
  <ul *ngFor="let t of item.technologies">
    <li>{{t.tech}}</li>
  </ul>
  </div>

https://stackblitz.com/edit/angular-3xsmgy?file=src/app/app.component.html https://stackblitz.com/edit/angular-3xsmgy?file=src/app/app.component.html

Hope it helps!希望能帮助到你!

projects.technologies is also an array, so you also need to iterate through them. projects.technologies 也是一个数组,因此您还需要遍历它们。

fe:铁:

<div *ngFor="let item of projectdata">
  <h5>{{item.projectName}}</h5>
  <ng-container *ngFor="let technologie of item.technologies">
    <span>{{technologie.tech}}</span>
  </ng-container>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM