以角度4循环遍历数组或json文件

时间:2017-09-22 07:31:54

标签: arrays json angular

我想在角度4中循环一个对象数组,我使用.ts文件将数组导出为posts.ts

中的变量
export var posts = [
  {
    "name":"art",
    "title":"Art",
    "items":[
      {
        "id": "1",
        "title":"Tooling Up",
        "author":"Amber Bravo",
        "date":"June 14 2015",
        "primaryColor":"#5a7785",
        "secondaryColor":"#455a64",
        "image":"https://g-design.storage.googleapis.com/production/v5/assets/tooling-up-header-a13cfd9a.svg",
        "desc":"How a new generation of prototyping tools at Google will help designers build better software.",
        "content":"# content goes here"
      },
      {
        "id": "2",
        "title":"Expressing Brand in Material",
        "author":"Viktor Persson & Rachel Been",
        "date":"July 4 2015",
        "primaryColor":"#202226",
        "secondaryColor":"#333",
        "image":"https://g-design.storage.googleapis.com/production/v5/assets/article_brand_2x1_202226-fc539618.svg",
        "desc":"Material design offers a system for designing functional and elegant software. How does your brand fit into the framework? We’ve created a step-by-step guide to staying on-brand while going material.",
        "content":"# content goes here"
      },
      {
        "id": "3",
        "title":"New Design Tools",
        "author":"Amber Bravo",
        "date":"July 29 2015",
        "primaryColor":"#3e50b4",
        "secondaryColor":"#303fc3",
        "image":"https://g-design.storage.googleapis.com/production/v5/assets/150727_GD_Article_ToolingUpFurther_1x1Tile-01-86c8e03e.svg",
        "desc":"See Also: (More) thoughts on design tools",
        "content":"# content goes here"
      }
    ]
  },
  {
    "name":"film",
    "title":"Film",
    "items":[
      {
        "id": "1",
        "title":"Design from iOS to Android (and Back Again)",
        "author":"Roman Nurik & Viltor Persson",
        "date":"Aug 20 2015",
        "primaryColor":"#3e50b4",
        "secondaryColor":"#303F9F",
        "image":"https://g-design.storage.googleapis.com/production/v5/assets/renditions/Article_iOS_to_Android_Header_3e50b4-f064882f-1240.png",
        "desc":"A practical guide to designing across platforms",
        "content":"# content goes here"
      },
      {
        "id": "2",
        "title":"Demystifying Density",
        "author":"Sebastien Gabriel",
        "date":"July 10 2015",
        "primaryColor":"#00ccb8",
        "secondaryColor":"#00b7a5",
        "image":"https://g-design.storage.googleapis.com/production/v5/assets/article_dpi_00ccb8-34fdd39e.svg",
        "desc":"Sebastien Gabriel takes one for the team with his exhaustive guide to DPI & PPI",
        "content":"# content goes here"
      },
      {
        "id": "3",
        "title":"Pixate and Form 1.3",
        "author":"Google Design",
        "date":"May 30 2015",
        "primaryColor":"#eeeeee",
        "secondaryColor":"#9e9e9e",
        "image":"https://g-design.storage.googleapis.com/production/v5/assets/pixate-and-form-1-3-header-2061f19f.svg",
        "desc":"Discover the latest features and start designing native prototypes on your device.",
        "content":"# content goes here"
      },
      {
        "id": "4",
        "title":"Welcome to the New Google Design",
        "author":"Google Design",
        "date":"Sep 10 2015",
        "primaryColor":"#3367d6",
        "secondaryColor":"#2755CC",
        "image":"https://g-design.storage.googleapis.com/production/v5/assets/Article_Welcome_Header_2880-ce3ec22d.svg",
        "desc":"More design, all the time",
        "content":" # content goes here"
      }
    ]
  },
  {
    "name":"photography",
    "title":"Photography",
    "items":[]
  },
  {
    "name":"design",
    "title":"Design",
    "items":[]
  },
  {
    "name":"topten",
    "title":"Top Ten",
    "items":[]
  },
  {
    "name":"aday",
    "title":"A Day in the Life",
    "items":[]
  }
]

然后我通常在app.componenet.ts导入它:

import { posts } from './posts';

现在我不知道如何循环它或如何加载json文件然后循环它我的意思是如何在html中循环显示app.componenet.html

内的数据

1 个答案:

答案 0 :(得分:3)

您必须传递此数组才能查看。

@Component(...)
class AppComponent {
    posts = posts;
...

然后在视图中你必须使用* ngFor指令。

<ng-container *ngFor="let post of posts">
{{ post.title }}
</ng-container>