简体   繁体   English

如何使用 Angular 7 在组件中显示嵌套 json 数组的项目

[英]How to display item of nested json array in component using Angular 7

How can I display "empName" and other details of this JSON in my table component in loop?如何在我的表组件中循环显示此 JSON 的“empName”和其他详细信息?

I'm using a third party API which provides a nested JSON object in return when I send employerID to the API URL.我正在使用第三方 API,当我将雇主 ID 发送到 API URL 时,该 API 提供嵌套的 JSON 对象作为回报。

After subscribing I'm storing the response in a var "AllPostedJobs"订阅后,我将响应存储在 var "AllPostedJobs" 中

{
    "status": "All Jobs",
    "result": [
        {
            "_id": "5c90fd3cfc7f3b0017803319",
            "job_title": "Web Designer",
            "job_desc": "test description ...",
            "location": "Mangalore",
            "experiance": "Freshers",
            "job_type": "Full-Time",
            "salary_package": "3L",
            "job_keywords": "Photoshop, Illustrator",
            "email_id": "hr@shreemithra.com",
            "employerID": "5c7e99c2a7a9eb00174de2b2",
            "company_name": "Shreemithra Designs",
            "AppliedStudentDetails": [
                {
                    "_id": "5c9393c1a918d60017de7e55",
                    "empName": "Anup",
                    "empID": "5c939375a918d60017de7e53",
                    "job_title": "Web Designer"
                }
            ],
            "__v": 1
        },
        {
            "_id": "5c913570cb78a100177ab23a",
            "job_title": "Full Stack Developer",
            "job_desc": "htjhsv dhsd jh jds fjshgdfkhsdmhfd;lw eiwiwemsd. This is a sample job description.",
            "location": "Pune",
            "experiance": "2 Years",
            "job_type": "Part-Time",
            "salary_package": "8L - 10L PA",
            "job_keywords": "Angular, Node JS, React, HTML5. CSS3",
            "email_id": "info@shreemithra.com",
            "employerID": "5c7e99c2a7a9eb00174de2b2",
            "company_name": "Shreemithra Designs",
            "AppliedStudentDetails": [
                {
                    "_id": "5c9393c9a918d60017de7e56",
                    "empName": "Anup",
                    "empID": "5c939375a918d60017de7e53",
                    "job_title": "Full Stack Developer"
                },
                {
                    "_id": "5ca60fa5ba17730017182ca8",
                    "empName": "Amit Pateriya",
                    "empID": "5c7795acfd39640017ca4c37",
                    "job_title": "Full Stack Developer"
                }
            ],
            "__v": 2
        }
    ]
}

You can bind display data by binding controls in an HTML template to properties of your component.您可以通过将 HTML 模板中的控件绑定到组件的属性来绑定显示数据。

The easiest way to display a component property is to bind the property name through interpolation.显示组件属性的最简单方法是通过插值绑定属性名称。 With interpolation, you put the property name in the view template, enclosed in double curly braces: {{AllPostedJobs.status}} .通过插值,您将属性名称放在视图模板中,用双花括号括起来: {{AllPostedJobs.status}}

<div  id="result-container" *ngFor="let record of AllPostedJobs.result">
   <another-component [record]= "record"></another-component>
</div>

Or depending on your need you can hand over entire result data to another-component.或者根据您的需要,您可以将整个结果数据交给另一个组件。

Now your another-component, should have @Input defined to handle the incoming data:现在你的另一个组件应该定义@Input来处理传入的数据:

export class AnotherComponent {
    @Input() record: Array<any>;

In your another-component template:在您的另一个组件模板中:

<div *ngFor="let student of record?.AppliedStudentDetails">
      <span>{{student.empName}}</span>
      <span [innerText]="student.job_title"></span>
    </div> 

The simplest way is when you receive data from api then send it to a function then apply multiples loop alter your data by add keys in front of data and that values for example最简单的方法是当您从 api 接收数据然后将其发送到一个函数然后应用多个循环通过在数据和值前面添加键来更改您的数据例如

var data = [{"id":1, "name":"smith","applicant":{"roll": 32,"class":10}}];
data[0].applicantRoll = data[0].applicant.roll;
data[0].applicantClass = data[0].applicant.class;

now you can apply *ngfor easily, try this.现在你可以轻松申请 *ngfor,试试这个。

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

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