【问题标题】:Ionic 4 sub array undefined while index is correctIonic 4 子数组未定义,而索引正确
【发布时间】:2020-04-04 20:42:07
【问题描述】:

我尝试使用console.log 查看值,但noticeSet2[index] 值未定义。当我打印 noticeSet 时,它会显示数组中的所有数据。我尝试在 console.log 中打印索引,它会显示与数组匹配的索引。

我尝试在主菜单的子菜单中创建一个子菜单,即 noticeSet 并且主菜单工作得很好。我尝试了类似的方法,但它不起作用。我现在需要解决的只是显示 noticeSet2[index] 值。谢谢

toggleItem(index, item) {

    //this.noticeSet2[index].open = !this.noticeSet2[index].open;

    console.log("here");
    console.log(this.noticeSet2[0]);
    console.log(this.noticeSet2[index]);
    console.log(index);

    let obj = {resident_id: this.resident_id,id: item.id}

    this.authService.postData(obj, "getSubCategoryList").then((result) =>{
      this.responseData = result;
        if(this.responseData.feedData){
          //this.noticeSet2[item.id] = this.responseData.feedData;
          this.noticeSet3[this.noticeSet2[item.id]] = this.responseData.feedData;
          this.isDataAvailable = true;
        //console.log(this.noticeSet);
        }
      }, (err) => {
      });
  }

<ion-list *ngFor="let item of noticeSet; let i = index;" lines="none"  class="accordion-list" detail="false"
    no-padding >

    <button ion-item tappble style="font-weight:bold;color:#424242;" (click)="getSubCategoryList(i,item)" [ngClass]="{'section-active':item.open,'section':!item.open}">
      <ion-icon item-right slot="end" name="arrow-forward" *ngIf="!item.open"
        style="font-size: 16px;font-weight: bold;"></ion-icon>
      <ion-icon item-right slot="end" name="arrow-down" *ngIf="item.open"
        style="font-size: 16px;font-weight: bold;color:rgb(74, 137, 220)"></ion-icon>
      {{ item.title }}
    </button>


    <div *ngIf="noticeSet2[item.id] && item.open">
      <ion-list *ngFor="let item of noticeSet2[item.id]; let j = index;" lines="none" class="child-list">
        <button ion-item  (click)="toggleItem(j,item)" *ngIf="item.title"
          [ngClass]="{'child-active':item.open, 'child':!item.open}">
          <ion-icon item-left slot="start" name="add" *ngIf="!item.open" style="font-size: 12px;font-weight: bold;">
          </ion-icon>
          <ion-icon item-left slot="start" name="close" *ngIf="item.open" style="font-size: 12px;font-weight: bold;">
          </ion-icon>
          <ion-label>
            {{ item.title }}
          </ion-label>
        </button>

        <div *ngIf="noticeSet3[this.noticeSet2[item.id]] && item.open">
         <ion-list  *ngFor="let item of noticeSet3[this.noticeSet2[item.id]]" class="child-list-list" lines="none">
          <ion-item class="child-item" *ngIf="item.title">
            <button ion-item >
              {{item.title}}
            </button>
          </ion-item>
        </ion-list>
      </div>

      </ion-list>
    </div>
  </ion-list>

【问题讨论】:

    标签: javascript html typescript ionic-framework ionic4


    【解决方案1】:

    这是因为你在 Promise 中使用了 value。 您有两种方法可以继续:

    第一个是使用

    .then( function() => { [...] }

    而不是

    .then((结果) => { [...] }

    它会起作用的。

    第二个是使用不在 {} 内的特殊变量。

        var that = this;
    // ...
    .then(function() { that.variable })
    

    希望对您有所帮助。

    【讨论】:

    • 感谢回复,我试过了,还是不行=3=
    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 2012-12-08
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2015-12-15
    • 2011-08-15
    • 1970-01-01
    相关资源
    最近更新 更多