I make one application with IONIC-2 Beta version. I want to use for-each loop. is it possible to use for each in angular-V2?
Thanks.
I make one application with IONIC-2 Beta version. I want to use for-each loop. is it possible to use for each in angular-V2?
Thanks.
First in the Component
, you have to declare the array you want to show:
import { Component } from "@angular/core";
@Component({
templateUrl:"home.html"
})
export class HomePage {
displayData : [];
constructor() {
this.displayData = [
{
"text": "item 1",
"value": 1
},
{
"text": "item 2",
"value": 2
},
{
"text": "item 3",
"value": 3
},
{
"text": "item 4",
"value": 4
},
{
"text": "item 5",
"value": 5
},
];
}
}
If you want to change the values in the code, you can do it by doing:
// We iterate the array in the code
for(let data of this.displayData) {
data.value = data.value + 5;
}
And then in your View you can print them like this:
<ion-content class="has-header">
<ion-list *ngFor="let data of displayData; let i = index" no-lines>
<ion-item>Index: {{ i }} - Text: {{ data.text }} - Value: {{ data.value }}</ion-item>
</ion-list>
</ion-content>
Please notice that part *ngFor="let data of displayData"
where:
displayData
is the array we defined in the Component
let data of ...
defines a new variable called data
, which represents each of the elements of the displayData
array.data
variable, and interpolation like {{ data.propertyName }}
.