简体   繁体   English

*ngFor 循环遍历数组

[英]*ngFor Looping over array

I have the following array:我有以下数组:

{
    "body": [{
        "coursename": "introto1",
        "course-lesson-name": "Welcome to One! "
    }, {
        "coursename": "introto2",
        "course-lesson-name": "What is One?"
    }, {
        "coursename": "introto2",
        "course-lesson-name": "What Can We do with One?"
    }]
}

and I am using the following *ngFor to iterate over it:我正在使用以下 *ngFor 对其进行迭代:

<div>
    <div class="col-sm-4" *ngFor="let product of products | keyvalue">
        <a>{{ products.body[0]['coursename'] }}</a>
    </div>
</div>

Obviously, the above is locked down to the first element only, what do I use instead of the element number (body[0]) to serve as as the iterable?显然,上面只锁定到第一个元素,我用什么来代替元素号(body[0])作为可迭代对象?

here is my component if its needed:如果需要,这是我的组件:

export class NavigationComponent implements OnInit {

  products;

  constructor(private dataService: MenuserviceService) { }

  ngOnInit() {

    this.dataService.sendGetRequest().subscribe((data: any[])=>{
      console.log(data);
      this.products = data;
    })  
  }

}

Use the product variable instead of products.body[0] and don't use the keyvalue pipe:使用product变量而不是products.body[0]并且不要使用keyvalue管道:

<div>
    <div class="col-sm-4" *ngFor="let product of products?.body">
        <a>{{ product['coursename'] }}</a>
    </div>
</div>

You would use keyvalue when you are iterating over an object's properties, not an array: KeyValuePipe当您迭代对象的属性而不是数组时,您将使用keyvalueKeyValuePipe

Check also this page about how to use NgForOf .同时检查该页面了解如何使用NgForOf

Error is because *NgFor works only for [Arrays] and [Objects with keyValue]错误是因为 *NgFor 仅适用于[Arrays][Objects with keyValue]

https://stackblitz.com/edit/angular-v812v6 https://stackblitz.com/edit/angular-v812v6

Check this out看一下这个

<div>
    <div class="col-sm-4" *ngFor="let product of response.body">
        <a>{{ product['coursename'] }}</a>
    </div>
</div>

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  response = {
    "body": [{
        "coursename": "introto1",
        "course-lesson-name": "Welcome to One! "
    }, {
        "coursename": "introto2",
        "course-lesson-name": "What is One?"
    }, {
        "coursename": "introto2",
        "course-lesson-name": "What Can We do with One?"
    }]
}
}

you don't need here keyvalue, body is an array of objects and ngFor is mostly for iterating thought arrays这里不需要键值,body 是一个对象数组, ngFor主要用于迭代思想数组

<div class="col-sm-4" *ngFor="let product of products?.body">
   <a>{{ product.coursename }}</a>
</div>

your subscription function parameter type is incorrect - any[], it is not an array, it is {body: any[]}, so I would say change it to any at least, or as it is TypeScript maybe create an interface that corresponds the response structure.您的订阅函数参数类型不正确 - any[],它不是数组,它是 {body: any[]},所以我会说至少将其更改为 any,或者因为它是 TypeScript 可能会创建一个对应的接口响应结构。

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

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