我对angularJs的递归有问题。

我想重复JSON文件中的所有名称:
    这是我的JSON文件结构:

var data = {
    "id": "580",
    "name": "test",
    "status": "ACTIVE",
    "parentId": null,
    "children": [
        {
            "id": "581",
            "name": "test 2",
            "status": "ACTIVE",
            "parentId": "580",
            "children": [{
                "id": "594",
                "name": "test 3",
                "status": "ACTIVE",
                "parentId": "581",
                "children": []
            }]
        }
    ]
}


我只想知道如何让ng-repeat的孩子成为孩子?

最佳答案

您应该使用ng-template才能递归使用。

<script type="text/ng-template" id="exampleTree">
{{ x.name }}
    <ul ng-if="x.children">
        <li ng-repeat="x in x.children" ng-include="'exampleTree'">
        </li>
    </ul>
</script>


要使用此模板并运行它:

<ul>
    <li ng-repeat="x in data" ng-include="'exampleTree'"></li>
</ul>


JSFiddle here

关于javascript - child 内的有 Angular child ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32936908/

10-09 06:19