I have problem inside array that looping inside array. This my HTML code:
<ion-content padding class="kuliner-index">
<ion-list *ngFor="let jen of jenis; let i = index">
<ion-list-header>{{jenisNama[i]}} </ion-list-header>
<ion-item *ngFor="let kuliner of kuliners[i]">
<img src="{{kuliner.main_img}}" style="width:50px;height:50px;margin:auto" >
{{kuliner.nama}}<br/>
<ion-icon name="md-star"></ion-icon> {{kuliner.rating}} / 5.00
</ion-item>
<br/><br/>
</ion-list>
</ion-content>
The problem is in kuliners[i] variable. Each seems like wrong access inside kuliners and each time i am refreshing kuliners[i]. It is like random access. I want for jenis[0], the kuliners that accessed is kuliners[0].
This is my ts file:
import {Page, NavController} from 'ionic-angular';
import {KulinerService} from '../../providers/kuliner-service/kuliner-service';
@Page({
templateUrl: 'build/pages/kuliner-index/kuliner-index.html',
providers: [KulinerService]
})
export class KulinerIndexPage {
public jenis : Number[] =null;
public jenisNama : String[] = null;
public jenisShow : boolean[] = null;
public kuliners: Array<any> = null;
private timer ;
constructor(public nav: NavController, public kulinerService : KulinerService) {
this.jenis = [1,2,3,4];
this.jenisNama = ['Appetizer (Hidangan Pembuka)','Main Course (Hidangan Utama)','Dessert (Hidangan Penutup)','Minuman Spesial'];
this.kuliners = new Array<any>();
this.jenisShow = new Array<boolean>();
this.loadKuliner();
}
loadKuliner()
{
for(var i=0;i<this.jenis.length ; i++)
{
alert(this.jenisNama[i]);
this.kulinerService.loadKulinerByJenis(this.jenis[i])
.then(dataResto => {
this.kuliners.splice(i, 0, dataResto);
this.jenisShow.push(false);
})
}
}
}
The last strange things is when I use the alert inside looping in loadKuliner() method the kuliners is accessed correctly. But if I remove the alert, the kuliners is become wrong again (like random).
Edit: The items in kuliners is like this:
[{"item_ID":"10009","nama":"Brownie Bite","main_img":"img\/Food\/Desserts_Brownie_Bite.jpg","harga":"40000","deskripsi":"Yummie...","points":"4","favorit":"2","rating":"4.25","rater":"1","tgl_post":"2016-01-15 00:00:00"}
kulinersarray looks like?