如何使用* ngFor显示多维数组?

时间:2019-01-04 12:48:39

标签: javascript ionic-framework typoscript

我刚开始使用js,ts,ionic

我在home.ts中得到了一个名为“商店”的数组:

0: {name: "wwwe", floor: 1}
1: {name: "weeqe", floor: -3}
2: {name: "weewqe", floor: -2}
3: {name: "qweqweqwe", floor: -2}

我也需要通过* ngFor显示它。我在home.html中有这个

<ion-card *ngFor="let store of stores">
<ion-card-header >
{{store.name}}
</ion-card-header>
</ion-card>

但这是显示空白页!

帮助请

我也尝试过这个:

<ion-card *ngFor="let store of stores; let i=index">
<ion-card-header >
{{store[i].name}}
</ion-card-header>
</ion-card>

不工作。

我读到必须将对象更改为数组,但是我不了解如何执行此操作。我需要将所有数组值放在一个位置。

home.ts:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {


constructor(public navCtrl: NavController, public http: Http) {
    var stores: Store[] = []
    var json;
    this.http.get('http/link/to/json').map(res => res.json()).subscribe(data => {
            json = data.data;
            for (var i of json.stores){
                stores.push({ 'name': i.name, 'floor': i.floor[0]  });
            }

        });

    console.log(stores);

  }


}

这是show console.log(商店):

https://i.stack.imgur.com/TGsys.png

1 个答案:

答案 0 :(得分:2)

您必须在构造函数外部声明stores变量

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {

// This way so you can reference it in the ngFor
stores: Store[] = [] 

constructor(public navCtrl: NavController, public http: Http) {
    var json;
    this.http.get('http/link/to/json').map(res => res.json()).subscribe(data => {
            json = data.data;
            for (var i of json.stores){
                this.stores.push({ 'name': i.name, 'floor': i.floor[0]  });
            }

        });

    console.log(this.stores);

  }


}