2016-07-22 58 views
7

我用IONIC-2 Beta版制作了一个应用程序。我想为每个循环使用。是否有可能在angular-V2中使用每个?For-each in ionic2 with angularjs2

谢谢。

+0

http://ionicframework.com/docs/v2/components/#lists – agriboz

+0

你想在哪里做HTML,或在TypeScript的foreach? –

回答

10

首先在Component,你必须声明你要显示的数组:

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 
     }, 
    ]; 
    } 
} 

如果要更改代码中的值,你可以通过做做到这一点:

// We iterate the array in the code 
for(let data of this.displayData) { 
    data.value = data.value + 5; 
} 

,然后在视图中,您可以打印出来是这样的:

<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> 

请注意部分*ngFor="let data of displayData"其中:

  • displayData是我们在Component
  • let data of ...定义的阵列定义了一个名为data新的变量,它表示每个displayData阵列的元件。
  • 我们可以通过使用data变量和插值如{{ data.propertyName }}来访问每个数组元素的属性。
+1

谢谢@sebaferreras ...你是天才.. –

+3

@sebaferreras我如何获得索引? –

+0

@RaghavRangani我已经编辑了答案,在'ngFor'中包含索引 – sebaferreras