so I'm practicing some Angular/Ionic and am having a bit of a hard time figuring out why my *ngFor does not display my data. It seems simple enough for me, but I guess I'm doing something terribly wrong.
So I have an item.interface.ts that looks like this:
export interface Item {
id: string;
name: string;
}
an items.ts that looks like this:
export default [
{
items:[
{
id:'1',
name:'Milk'
},
{
id:'2',
name:'Wheat'
},
{
id:'3',
name:'Cheese'
}
]
}
];
Then I have my home.ts:
import { Component, OnInit } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Item } from '../../data/item.interface';
import items from '../../data/items';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage implements OnInit {
itemCollection: {items: Item[]}[];
constructor(public navCtrl: NavController,
private navParams: NavParams){}
ngOnInit(){
this.itemCollection = items;
}
}
And finally my home.html:
<ion-header>
<ion-navbar>
<ion-buttons start>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
</ion-buttons>
<ion-title>Add Items</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-row>
<ion-col text-center>
<form>
<label for="item">Type an item/ingredient name</label>
<input type="text" name="item"><br>
<button ion-button small type="submit">Add Item</button>
<button ion-button small color="danger">Empty List</button>
</form>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-list>
<button ion-item *ngFor="let singleItem of itemCollection">
<p>{{singleItem.name}}</p>
</button>
</ion-list>
</ion-col>
</ion-row>
<ion-row>
<ion-col text-center>
<button ion-button medium block>Scan</button>
</ion-col>
</ion-row>
</ion-content>
So when I do that ngFor in my home.html, it simply prints nothing, instead of the singleItem.name
Just as a curiosity, if I try to print singleItem.items.length, it does return 3, which looks correct, but for some reason it won't return the values from my data. Could you anyone help me with this, please? Cheers guys!