Javascript 使用json数组的Angularjs if-else条件

Javascript 使用json数组的Angularjs if-else条件,javascript,json,angularjs,Javascript,Json,Angularjs,我正在使用Angularjs,我正在从json文件填充数据。我正在使用ng repeat显示数据。有一个类别、子类别和产品。有些类别有子类别,有些类别有一些产品。我试图写一个条件,比如如果类别有子类别显示子类别,如果有产品则显示产品 [{ "category":"Cables", "subCategories":[ { "subCategory":"Sub Category 1" }, {

我正在使用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>  
  • {{chartValue.category}
    • {{item.subCategory}
如果有人知道怎么做?请建议

为子菜单创建了两个模板

  <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>
  • {{item.subCategory}
  • {{item.proTitle}}

感谢您提供的解决方案。。令人惊叹的。。