繁体   English   中英

如何从angularJS中的对象数组中获取数据?

[英]How to fetch data from array of objects in angularJS?

这是我的json-

{
    topalbums: {
      album: [
        {
          name: "The Very Best of Cher",
          playcount: 1634402,
          mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5",
          url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher",
          artist: {
            name: "Cher",
            mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818",
            url: "http://www.last.fm/music/Cher"
          }
        }
      ]
    }
}

如何使用角度控制器通过循环获取每个即将出现的对象的artist.name的数据?

尝试this.in控制器中,您可以像这样使用angular foreach。

 var app = angular.module("app",[]) app.controller('ctrl',['$scope', function($scope){ $scope.data={ topalbums: { album: [ { name: "The Very Best of Cher", playcount: 1634402, mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5", url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher", artist: { name: "Cher", mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818", url: "http://www.last.fm/music/Cher" } }, { name: "The Very Best of Cher", playcount: 1634402, mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5", url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher", artist: { name: "Cher2", mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818", url: "http://www.last.fm/music/Cher" } } ] } }; angular.forEach($scope.data.topalbums,function(album){ angular.forEach(album,function(a){ alert(a.artist.name); console.log(a.artist.name); }) }) }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <div class="item item-checkbox"> <div ng-repeat="album in data.topalbums"> <p ng-repeat="a in album">{{a.artist.name}}</p> </div> </div> 

假设您在$scope.allAlbums有此json。

您可以使用来迭代所有专辑的歌手姓名

$scope.allAlbums.topalbums.album.forEach(function(item) {

    console.log(item.artist.name)
})

您可以尝试一下。 名称将包含数组相册名称:

var names = data.topalbums.album.map(function(item){
               return item.artist.name;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM