0

我正在使用 Angularjs,并且正在从 json 文件中填充数据。我正在使用 ng-repeat 来显示数据。有一个类别、子类别和产品。有些类别有子类别,有些类别有一些产品。我正在尝试编写条件,例如类别是否有子类别显示子类别,如果有产品则显示产品。

[{
    "category":"Cables",
    "subCategories":[
        {
            "subCategory":"Sub Category 1"

        },
        {
            "subCategory":"Sub Category 2"
        }
    ]
},
{
    "category":"Wallplates & Boxes",
    "subCategories":[
        {
            "subCategory":"Sub Category 6"
        },
        {
            "subCategory":"Sub Category 7"
        }
    ]
},
{
    "category":"Media Distribution",
    "products":[
        {
            "proTitle":"Product 1"
        },
        {
            "proTitle":"Product 2"
        }
    ]
}]

控制器在下面。

var app = angular.module('analytics', []);
app.controller ('categoryCtrl', function ($scope, $http){
$http.get('json/category_data_new.json').success(function(data) {
    $scope.chartValues = data;
});

});

我正在使用下面的代码来显示类别和子类别

<ul class="nav">
    <li ng-repeat="chartValue in chartValues">
        <span ng-click="loadRecord(chartValue, $index)">
           {{chartValue.category}}
        </span>
        <ul ng-show="chartValue.subCatList">
            <li ng-repeat="item in chartValues[$index].subCategories">
                  <span ng-click="loadSubRecord(item, $index)">
                      {{item.subCategory}}
                   </span>
            </li>
        </ul>
    </li>
</ul>  

如果有人知道如何做到这一点?请建议。

4

1 回答 1

0

在此处检查此链接

为子菜单创建了两个模板

  <ul ng-show="chartValue.subCategories">
        <li ng-repeat="item in chartValues[$index].subCategories">
              <span ng-click="loadSubRecord(item, $index)">
                  {{item.subCategory}}
               </span>
        </li>
    </ul>
     <ul ng-show="chartValue.products">
        <li ng-repeat="item in chartValues[$index].products">
              <span ng-click="loadSubRecord(item, $index)">
                  {{item.proTitle}}
               </span>
        </li>
    </ul>
于 2013-10-21T13:04:25.677 回答