7

我已将值存储在两个数组中以在单个 ion-list 中进行迭代。Billerstatusstate 和 Billerstatusnamelst 是两个数组。

我尝试了以下迭代

<ion-list ion-item *ngFor="let stat of Billerstatusstate;let bil of Billerstatusnamelst "  >

  {{bil}} <button round>{{stat}}</button>

</ion-list>

<ion-list ion-item *ngFor="let stat of Billerstatusstate" *ngFor="let bil of Billerstatusnamelst "  >

  {{bil}} <button round>{{stat}}</button>

</ion-list>

它为两个局部变量取第一个迭代值。

有什么我想念的吗?

有没有其他方法可以将两个值存储在单个数组中并使用 ngFor 将其拆分到 View 端。

4

5 回答 5

9

我可以创建一个管道来将你的两个数组“合并”成一个数组,然后你可以迭代这个新数组。

这是一个示例:

@Pipe({
  name: 'merge'
})
export class MergePipe {
  transform(arr1, arr2) {
    var arr = [];
    arr1.forEach((elt, i) => {
      arr.push({ state: elt, name: arr2[i] });
    });
  }
}

并以这种方式使用它:

<ion-list ion-item *ngFor="let elt of Billerstatusstate | merge:Billerstatusnamelst"  >
  {{elt.name}} <button round>{{elt.state}}</button>
</ion-list>    
于 2016-06-29T13:34:47.610 回答
7

为什么不这样做

<ion-list ion-item *ngFor="let stat of Billerstatusstate;let bil of Billerstatusnamelst "  >

  {{bil}} <button round>{{stat}}</button>

</ion-list>

array你不要在你的代码中创建一个:

// I'm assuming they both have the same size
for (var _i = 0; _i < Billerstatusstate.length; _i++) {
    this.singleArray.push({
                         stat: this.Billerstatusstate[_i],
                         bil: this.Billerstatusnamelst[_i] 
                        });
}

然后在您的页面中:

<ion-list ion-item *ngFor="let item of singleArray;"  >

  {{item.bil}} <button round>{{item.stat}}</button>

</ion-list>
于 2016-06-29T14:06:26.143 回答
6

您可以利用ngFor指令的索引来实现这一点。

<ion-list ion-item *ngFor="let stat of Billerstatusstate; let i=index">

  {{Billerstatusnamelst[i]}} <button round>{{stat}}</button>

</ion-list>
于 2016-06-29T13:28:46.247 回答
2

我遵循了这种方法。我将值存储在单个数组中,而不是两个数组中,因为它来自单个源。

 this.Billerstatusnamelst.push({name:"testname",Status:"Failure"});

在 HTML 部分

 <ion-list ion-item *ngFor="let bil of Billerstatusnamelst "  >

  {{bil.name}} <button round>{{bil.Status}}</button>

</ion-list>

它对我有用..

于 2016-06-29T13:58:54.130 回答
0

单个数组中的多个数组正在使用

    this.termlist = this.result.termlst.$values;
    this.disableterm = this.result.disableterms.$values;
    this.customlist = this.result.customgrplist.$values;

单数组代码

 this.totalarrayob =[ this.termlist,this.disableterm, this.customlist];

我如何使用 *ngfor 在 html 中调用

<ion-col col-12 no-padding *ngFor="let list of totalarrayob;let k = index">
    <h6>lorem Ipsum</h6>
    <ion-list>
      <ion-item>
        <ion-label>I Term</ion-label>
        <ion-checkbox checked="false"></ion-checkbox>
        <ion-note item-right>
          25000
        </ion-note>
      </ion-item>
    </ion-list>
  </ion-col>
于 2018-07-20T07:34:28.733 回答