AngularJS: ng-repeat 使用对象数组的数组字段

AngularJS: ng-repeat using an array field of an array of objects

我有一个 json 结构:

let total_price = "£100";

let product_groups = [
    {
        "name": "group 1",
        "summary": "group 1 summary",
        "products": [
            {
                "name": "product 1",
                "price": "£10",
                ...
            }, 
            ...
        ]
    },
    ...
]

我正在尝试将其展平并使用 table 以下列形式显示它

<table class="table">
    <thead>
        <tr>
            <th>Group Name</tr>
            <th>Product Name</tr>
            <th>Product Price</tr>
        </tr>
     </thead>
     <tbody>
         <tr ng-repeat="group in product_groups">
             <td>{{group.name}}</td>
             <td>{{product.name}}</td>
             <td>{{product.price}}<td>
         </tr>
         <tr>
             <th colspan="2">Total</th>
             <td>{{total_price}}</td>
         </tr>
     </tbody>
</table>

但这显然不能让我访问每个产品。有没有办法使用 ng-repeat 来做到这一点?或者我需要先在控制器中展平我的数据结构吗?

在现有的内部使用新的 ng-repeat,就像嵌套循环一样。

您好,请尝试以下解决方案

    $scope.newArray = [];

    let product_groups = [
        {
            "name": "group 1",
            "summary": "group 1 summary",
            "products": [
                {
                    "name": "product 1",
                    "price": "£10",
                }, 
            ]
        },
    ];

for(var i = 0; i< product_groups.length; i++) {
   for(var j =0; j< product_groups[i].products.length; j++) {
    var flattenProduct = {
                           gname:product_groups[i].name, 
                           name:product_groups[i].products[j].name,
                           price : product_groups[i].products[j].price
                        };
        $scope.newArray.push(flattenProduct );
 }

}

然后像下面这样调用 html:

<table class="table">
    <thead>
        <tr>
            <th>Group Name</tr>
            <th>Product Name</tr>
            <th>Product Price</tr>
        </tr>
     </thead>
     <tbody>
         <tr ng-repeat="p in newArray">
             <td>{{p.gname}}</td>
             <td>{{p.name}}</td>
             <td>{{p.price}}<td>
         </tr>
     </tbody>
</table>

你应该弄平你的 JSON。我建议更改您的结构:

product_groups = [{
  "name": "group 1",
  "summary": "group 1 summary",
  "products": [{
      "name": "product 1",
      "price": "£10"
    },
    {
      "name": "product 2",
      "price": "£20"
    }
  ]
}]

至:

product_groups = [{
    "name": "group 1",
    "summary": "group 1 summary",
    "product": {
      "name": "product 1",
      "price": "£10"
    }
  },
  {
    "name": "group 1",
    "summary": "group 1 summary",
    "product": {
      "name": "product 2",
      "price": "£20"
    }
  }
]

那么你可以简单地使用这个例子:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.product_groups = [{
      "name": "group 1",
      "summary": "group 1 summary",
      "product": {
        "name": "product 1",
        "price": "£10",
      }
    },
    {
      "name": "group 1",
      "summary": "group 1 summary",
      "product": {
        "name": "product 2",
        "price": "£20",
      }
    }
  ]
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="myCtrl">

    <table class="table">
      <thead>
        <tr>
          <th>Group Name</th>
          <th>Product Name</th>
          <th>Product Price</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="group in product_groups">
          <td>{{group.name}}</td>
          <td>{{group.product.name}}</td>
          <td>{{group.product.price}}
            <td>
        </tr>
      </tbody>
    </table>

  </div>

</body>

</html>

您可以使用嵌套的 ng-repeat' and useng-repeat-startandng-repeat-end` 尝试以下操作。

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
 $scope.product_groups = [ { "name": "group 1", "summary": "group 1 summary", "products": [ { "name": "product 1", "price": "£10", }, { "name": "product 1", "price": "£10", }, ] }, { "name": "group 2", "summary": "group 2 summary", "products": [ { "name": "product 21", "price": "£20", }, { "name": "product 15", "price": "£80", }, ] } ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="plunker">
  <div ng-controller="MainCtrl">
    <table class="table">
    <thead>
        <tr>
            <th>Group Name</th>
            <th>Product Name</th>
            <th>Product Price</th>
        </tr>
     </thead>
     <tbody>
        <tr ng-repeat-start="group in product_groups"></tr>
         <tr  ng-repeat="product in group.products">
             <td>{{group.name}}</td>
              <td>{{product.name}}</td>
              <td>{{product.price}}</td>
         </tr>
        <tr ng-repeat-end></tr>
     </tbody>
</table>
  </div>
</body>