简体   繁体   English

Angular2迭代ngFor JSON文件

[英]Angular2 Iterating ngFor JSON-File

I´m fairly new to Angular2, and i want to read out a json file. 我是Angular2的新手,我想读出一个json文件。 It´s working, that I get the file from a REST-Client, i can save the file in a local variable in a component. 我可以从REST客户端获取文件,我可以将文件保存在组件的局部变量中。 Now I´m trying to read properties (Array) with ngFor, but this isn´t working. 现在,我正在尝试使用ngFor读取属性(数组),但这不起作用。 Here´s the html: 这是html:

<div *ngFor="let categories of tasks.tasks">
    {{categories}} --> Display name of categorie (epg, recs)
    <p *ngFor="let task of categories">{{task.text}}</p>
</div>

And the JSON-File: 和JSON文件:

{
    "tasks": {
        "epg": [{
            "text": "\\\\...\\Daten\\Videos\\Aufnahmen"
        }, {
            "text": "C:\\Users\\...\\Desktop"
        }],
        "recs": [{
            "text": "\\\\...\\Daten\\Videos\\Aufnahmen"
        }, {
            "text": "C:\\...\\Junias\\Desktop"
        }]
    }
}

Hope someone can help me ;) 希望可以有人帮帮我 ;)

Consider 考虑

z={
"tasks": {
    "epg": [{
        "text": "\\\\...\\Daten\\Videos\\Aufnahmen"
    }, {
        "text": "C:\\Users\\...\\Desktop"
    }],
    "recs": [{
        "text": "\\\\...\\Daten\\Videos\\Aufnahmen"
    }, {
        "text": "C:\\...\\Junias\\Desktop"
    }]
}

Modify your JSON as 将您的JSON修改为

  let a=this.z.tasks;
  this.z = Object.keys(a).map(function(k) { return a[k] });

Here is a plunker 这是一个矮人

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

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