读取单个数组并在离子应用程序中显示为列表
Read a single array and display as list in ionic app
在 Ionic 应用程序中,我有一个如下所示的数组。
以下来自console.log(this.itemlist);
0: Array(4)
0: "Item1"
1: "Item2"
2: "Item3"
3: "Item4"
length: 4
问题是我无法在我的 ionic 应用程序的列表视图中将单个记录显示为单个项目。
我的 item.html 如下所示:
<ion-item no-lines text-wrap class="item" *ngFor="let item of itemlist" >
<p>{{item}}</p>
</ion-item>
我的 item.ts 如下所示:
if(localStorage.length > 0){
var localStorageArray = new Array();
for (i=0;i<localStorage.length;++i){
localStorageArray[i] = localStorage.key(i)+localStorage.getItem(localStorage.key(i));
}
var sortedArray = localStorageArray.sort();
}
this.itemlist.push(sortedArray);
console.log(this.itemlist);
你 this.itemlist
的结构有误.. 将你的 *ngFor 改成这个 *ngFor="let item of itemlist[0]"
当您将一个值压入数组时,它会作为一个新项插入到数组中。在您的情况下,整个 sortedArray
将成为您的 itemlist
[= 中的第 0 个元素15=]
另一种解决方案是将数组散布在项目列表中,而不是像这样使用 .push
this.itemlist = [...sortedArray]
在 Ionic 应用程序中,我有一个如下所示的数组。
以下来自console.log(this.itemlist);
0: Array(4)
0: "Item1"
1: "Item2"
2: "Item3"
3: "Item4"
length: 4
问题是我无法在我的 ionic 应用程序的列表视图中将单个记录显示为单个项目。
我的 item.html 如下所示:
<ion-item no-lines text-wrap class="item" *ngFor="let item of itemlist" >
<p>{{item}}</p>
</ion-item>
我的 item.ts 如下所示:
if(localStorage.length > 0){
var localStorageArray = new Array();
for (i=0;i<localStorage.length;++i){
localStorageArray[i] = localStorage.key(i)+localStorage.getItem(localStorage.key(i));
}
var sortedArray = localStorageArray.sort();
}
this.itemlist.push(sortedArray);
console.log(this.itemlist);
你 this.itemlist
的结构有误.. 将你的 *ngFor 改成这个 *ngFor="let item of itemlist[0]"
当您将一个值压入数组时,它会作为一个新项插入到数组中。在您的情况下,整个 sortedArray
将成为您的 itemlist
[= 中的第 0 个元素15=]
另一种解决方案是将数组散布在项目列表中,而不是像这样使用 .push
this.itemlist = [...sortedArray]