【问题标题】:Json Lists in arrays ionic return list values数组中的 Json 列表离子返回列表值
【发布时间】:2023-03-10 08:59:02
【问题描述】:

我是 ionic 新手,我希望能够扩展一个简单的 json 数据集以在数组中包含列表: 我的 json 文件如下所示:

angular.module('starter.services', [])

/** A simple example service that returns some data. */
.factory('Bands', function() {

  var bands = [
         {"id": "0", name: 'U2', nationality: 'Irish', category: 'Rock', pic: "U2.jpg", url:"www.u2.com" },                     
       {
            "albums":
                {"album"
                    [
                        { "id": "101", name:"Songs Of Innocence", year:"2014", pic: "u2_soi_cover.jpg" },
                        { "id": "102", name:"No Line On The Horizon", year:"2009", pic: "u2_nloth_cover.jpg" },
                        { "id": "103", name:"How To Dismantle An Atomic Bomb", year:"2004", pic: "u2_htdaab_cover.jpg" },
                     ]
                },
      },
   {"id": "1", name: 'Silverchair', nationality: 'Australian', category: 'Grunge', pic: "silverchair.jpg", url:"www.silverchair.com/" },                        
       {
            "albums":
                {"album"
                    [
                        { "id": "102", name:"Frogstomp", year:"1995", pic: "sc_frogstomp_cover.jpg" },
                    ]
                },

},
 ];

return {
all: function() {
  return bandss;
},
get: function(bandId) {
  // Simple index lookup
  return bands[bandId];
  }
 }
 })

所以我已经能够使用重复返回乐队列表并传递乐队 ID 以显示各个乐队的详细信息。

我不想将乐队页面扩展到它以返回专辑列表详细信息,所以我猜它会是这样的,但我需要一些帮助来了解如何从特定乐队的数组中取出列表ID。

 <ion-content>
      <div class="details">
      <img src="pics/bands/{{band.pic }}" />
    <h2>{{band.name}}</h2>
   <p>{{band.nationality}}</p>
   </div>
  <ion-list>
      <ion-item ng-repeat="album in albums" type="item-text-wrap" >
             <h2>{{album.name}}</h2>
      <p>{{album.year}}</p>
        </ion-item>
    </ion-list>
   </ion-content>

任何能帮助我指明正确方向的方法都会很棒。

【问题讨论】:

    标签: json angularjs ionic-framework


    【解决方案1】:

    你需要改变json格式,将“专辑”移动到“乐队”中,像这样:

    [
      {
        "id": "0",
        "name": "U2",
        "nationality": "Irish",
        "category": "Rock",
        "pic": "U2.jpg",
        "url": "www.u2.com",
        "albums": [
            {
                "id"": "101",
                "name": "SongsOfInnocence",
                "year": "2014",
                "pic": "u2_soi_cover.jpg"
            }
        ]
      }
    ]
    

    现在在您的视图中重复:

    <ion-item ng-repeat="album in band.albums" type="item-text-wrap" >
      <h2>{{album.name}}</h2>
      <p>{{album.year}}</p>
    </ion-item>
    

    【讨论】:

    • 谢谢你,这很完美,这正是我所需要的,现在我觉得它很有意义。
    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多