7
A
回答
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
相關問題
- 1. 在Angularjs2/Ionic2中實現pdfmake
- 2. Ionic2 with angular2 issue while using ion-select with ngFor
- 3. Pdf generation in ionic2
- 4. ngFormModel in ionic2
- 5. Foreach in foreach [PHP]
- 6. ForerunnerDB AngularJS2模塊
- 7. LINQ forEach with Replace
- 8. Angulars with ionic2工作教程
- 9. c#foreach with Action.BeginInvoke
- 10. C#Foreach with MySQL
- 11. Toach with foreach loop
- 12. ForEach with EditorFor
- 13. FOREACH in cypher - neo4j
- 14. 」with「macro in C
- 15. WITH&UNNEST in BigQuery
- 16. Upsert with $ in
- 17. 使用Typescript的Ionic2和AngularJS2應用程序找不到類型
- 18. R。foreach with .combine = rbindlist
- 19. Powershell foreach in pssession
- 20. forEach in angular
- 21. forEach in JavaScript
- 22. AS3 dispatchEvent in forEach
- 23. foreach:「in」v。「as」
- 24. Foreach in Linq
- 25. Bug in Knockout JS with <select> foreach循環內的元素
- 26. scala foreach of 2-D List/Array in chisel with types issue
- 27. ASP.MVC5 - Foreach with property array
- 28. foreach with jquery元素
- 29. php foreach with javascript script
- 30. pushViewController with tableViewController in viewController
http://ionicframework.com/docs/v2/components/#lists – agriboz
你想在哪裏做HTML,或在TypeScript的foreach? –