Angularjs if else使用json数组条件

时间:2013-10-21 12:50:25

标签: javascript json angularjs

我正在使用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>  

如果有人知道这是怎么回事?请建议。

1 个答案:

答案 0 :(得分:0)

Check this link here

已为子菜单创建了两个模板

  <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>