使用AngularJS访问嵌套的JSON

sca*_*e13 22 javascript mobile json angularjs cordova

我正在尝试使用angular.js,hammer.js和topcoat来制作和移动webapp.

我在显示像这样的Json文件中的数据时遇到了一些麻烦:

{
"version": "1",
"artists": {
    "artist1": {
        "name": "artist1name",
        "jpeg": "../img/artist/artist1.jpg",
        "albums": {
            "album1": {
                "type": "cd",
                "title": "titlealbum1",
                "subtitle": "subtitlealbum1",
                "jpeg": "../img/album1.jpg",
                "price": "12.00",
                "anystring1": "anystring1album1",
                "anystring2": "anystring2album1"
            },
            "album2": [{
                "type": "cd",
                "title": "titlealbum2",
                "subtitle": "subtitlealbum2",
                "jpeg": "../img/album2.jpg",
                "price": "12.00",
                "anystring1": "anystring1album2",
                "anystring2": "anystring2album2"
            }],
            "album3": [{
                "type": "cd",
                "title": "titlealbum3",
                "subtitle": "subtitlealbum3",
                "jpeg": "../img/album3.jpg",
                "price": "13.00",
                "anystring1": "anystring1album3",
                "anystring2": "anystring2album3"
            }]
        }
    },
    "artist2": {
        "name": "artist2name",
        "jpeg": "../img/artist/artist2.jpg",
Run Code Online (Sandbox Code Playgroud)

我的js文件是这样的:

angular.module('list', [])
function ListCtrl ($scope, $http) {
$http({method: 'GET', url: 'json/json_price_1.json'}).success(function(data)
{
$scope.artists = data.artists; // response data 
$scope.albums = data.artists.albums; /this is where im getting trouble
});        
};
Run Code Online (Sandbox Code Playgroud)

我的HTML文件是这样的:

<body ng-app="list">

<h3>Titulos</h3>

<div ng-controller="ListCtrl">
<ul ng-repeat="artist in artists">
        <li >{{artist.name}}</li>

    </ul>
</div>

<div ng-controller="ListCtrl">
<ul ng-repeat="album in albums">
        <li >{{album.title}}</li>  //not working here. 

    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

我想显示所有相册,如果我的用户选择了特定的艺术家,我想过滤这些相册.这里的问题是如何在这个嵌套的json上选择.顺便说一句,artist.name正确显示.

第二个问题是,我将如何使用文本字段过滤这些艺术家.

Ada*_*dam 14

我建议像这样:

$http({method: 'GET', url: 'json/json_price_1.json'}).success(function(data) {
    $scope.artists = [];
    angular.forEach(data.artists, function(value, key) {
        $scope.artists.push(value);
    });
    $scope.isVisible = function(name){
        return true;// return false to hide this artist's albums
    };
});
Run Code Online (Sandbox Code Playgroud)

然后:

<div ng-controller="ListCtrl">
    <ul>
        <li ng-repeat="artist in artists">{{artist.name}}</li>
    </ul>
    <ul ng-repeat="artist in artists" ng-show="isVisible(artist.name)">
        <li ng-repeat="album in artist.albums">{{album.title}}</li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)


ivo*_*szz 7

您的所有问题都以您的JSON开头.我建议你简化你的JSON,让每个ArtistAlbum对象和ArtistsAlbums对象的数组.

试试这个:


{
  "version": "1",
  "artists": [
    {
      "name": "artist1name",
      "jpeg": "../img/artist/artist1.jpg",
      "albums": [
        {
          "type": "cd",
          "title": "titlealbum1",
          "subtitle": "subtitlealbum1",
          "jpeg": "../img/album1.jpg",
          "price": "12.00",
          "anystring1": "anystring1album1",
          "anystring2": "anystring2album1"
        },
        {
          "type": "cd",
          "title": "titlealbum2",
          "subtitle": "subtitlealbum2",
          "jpeg": "../img/album2.jpg",
          "price": "12.00",
          "anystring1": "anystring1album2",
          "anystring2": "anystring2album2"
        },
        {
          "type": "cd",
          "title": "titlealbum3",
          "subtitle": "subtitlealbum3",
          "jpeg": "../img/album3.jpg",
          "price": "13.00",
          "anystring1": "anystring1album3",
          "anystring2": "anystring2album3"
        }
      ]
    },
    {
      "name": "artist2name",
      "jpeg": "../img/artist/artist2.jpg",
      "albums": []
    }
  ]
}

在我的示例中,数组可以为空(artist2没有分配相册).

您的错位也是错误的,在您ListCtrl知道正确之前,您无法分配和显示相册Artist.如果您想要显示所有艺术家的所有专辑,您需要遍历所有艺术家.

示例控制器:


angular.module('list', []);

function ListCtrl($scope, $http) {
  $http({
    method: 'GET',
    url: 'json_price_1.json'
  }).success(function(data) {
    $scope.artists = data.artists; // response data
    $scope.albums = [];
    angular.forEach(data.artists, function(artist, index) {
      angular.forEach(artist.albums, function(album, index){
        $scope.albums.push(album);
      });
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

这里是基于jessie示例修改的Plunkr:http://plnkr.co/edit/zkUqrPjlDYhVeNEZY1YR?p = preview


小智 5

检查柱塞链接

回答您的问题“我如何选择此嵌套json?”

$.each(data.artists, function(index, element){
  $.each(this.albums, function(index, element){
    $scope.albums.push(element);
  });
});
Run Code Online (Sandbox Code Playgroud)

回答您的问题“我将如何用文本字段过滤那些艺术家?”

<input ng-model="searchText.artistName">

<ul ng-repeat="album in albums | filter:searchText">
    <li>{{album.title}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢!您的例子很有启发性。我是这个方面的新手,您的回答很棒! (2认同)