【问题标题】:why i'm unable to loop through my object of array为什么我无法遍历我的数组对象
【发布时间】:2018-03-08 18:17:46
【问题描述】:

我正在尝试显示object of array 中的所有数据。

但我无法显示其中任何一个,为什么?

这是我尝试过的

在这里演示:https://stackblitz.com/edit/ionic-dsdjvp?file=pages%2Fhome%2Fhome.ts

home.html

  <div *ngIf="searchResults.length > 0">
     <ion-item *ngFor="let searchResult of searchResults.details">          
        <ion-avatar item-start>
          <!--<img src="img/Rabbit-Cage.jpg">  -->  
        </ion-avatar>
        <h2>searchResult.user_id</h2>
        <h3>searchResult.email</h3>
        <p>searchResult.token</p>         
      </ion-item>     
</div>

home.ts

export class HomePage {

  searchResults = [{
    "details": [{
        "user_id": "73",
        "email": "ejazanwar777@gmail.com",
        "token": "217808036f0215fee13aee8925574899"
    }, {
        "user_id": "94",
        "email": "ejazanwar7dds77@gmail.com",
        "token": "f4e9a701f9dae581ecbc2abf1470f88f"
    },  {
        "user_id": "98",
        "email": "ejazanwar777dsggds@gmail.com",
        "token": "3481a3c17a752ef136a4eadeaa0813f4"
    }, {
        "user_id": "99",
        "email": "ejazanwar777@gmail.comsaasas",
        "token": "6bd7941c8b501f798c8854047a395bdf"
    }, {
        "user_id": "100",
        "email": "ejazanwar777jgfjgfjfg@gmail.com",
        "token": "127934324e39ad681826322632f91259"
    }, {
        "user_id": "107",
        "email": "ejazanwar77adsfdasf7@gmail.com",
        "token": "1a6c860fc6d6a2875f4de371dee22b22"
    }, {
        "user_id": "108",
        "email": "ejazanwar777@gmail.comdddd",
        "token": "684aae9542ddc76f1f78c4360afe8846"
    }, {
        "user_id": "109",
        "email": "jhddfhfdfdhdhfjhj@gmail.com",
        "token": "069fe4f0ec2210c1e8eb9400486f1718"
    }, {
        "user_id": "110",
        "email": "ejazanwar777@gmail.comssdssdd",
        "token": "a10f3620d18466a0d5780749401052ad"
    }]
}];

  constructor(public navCtrl: NavController) {

  }

}

这是我的演示 https://stackblitz.com/edit/ionic-dsdjvp?file=pages%2Fhome%2Fhome.ts

请帮我提前谢谢!!!

【问题讨论】:

  • 你不需要 *ngIf,删除它,或者试试 searchResults?.details 或 *ngIf="searchResults"

标签: angular typescript ionic3


【解决方案1】:

你在这里做错了searchResults.length &gt; 0。你需要像这样使用searchResults?.details?.length &gt; 0

这是工作的stackblitz

  <div *ngIf="searchResults?.details?.length > 0">
     <ion-item *ngFor="let searchResult of searchResults?.details">         
        <ion-avatar item-start>
          <!--<img src="img/Rabbit-Cage.jpg">  -->  
        </ion-avatar>
        <h2>searchResult.user_id</h2>
        <h3>searchResult.email</h3>
        <p>searchResult.token</p>         
      </ion-item>     
</div>

没有*ngIf

 <div>
         <ion-item *ngFor="let searchResult of searchResults?.details">         
            <ion-avatar item-start>
              <!--<img src="img/Rabbit-Cage.jpg">  -->  
            </ion-avatar>
            <h2>searchResult.user_id</h2>
            <h3>searchResult.email</h3>
            <p>searchResult.token</p>         
          </ion-item>     
    </div>

【讨论】:

  • 您需要将数据格式化为嵌套的JSON 数组。然后只有您可以使用它。即myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] }, { "name":"BMW", "models":[ "320", "X3", "X5" ] }, { "name":"Fiat", "models":[ "500", "Panda" ] } ] }
  • 很高兴听到它有帮助:)
猜你喜欢
  • 1970-01-01
  • 2017-05-20
  • 2011-02-02
  • 2012-09-27
  • 2019-04-10
  • 2020-10-29
  • 1970-01-01
  • 2023-01-05
相关资源
最近更新 更多