在角度视图中渲染递归二叉树json

时间:2016-04-29 16:06:27

标签: javascript angularjs json recursion

我有一个递归的json结构,如下所示,我想以角度动态渲染html视图模板。

关键如下:只要它具有属性对象,它可能具有属性对象对象,其中对象始终是一个数组,对象始终是一个对象。它可能会无限循环,具体取决于json。

我已经将视图定义如下,但是我在视图中定义的深度有限,而视图应该根据json呈现。

的index.html

<div ng-repeat="subtopic in json.subtopic">
    <div ng-if="subtopic.objects">
        <div ng-repeat="subtopic in subtopic.objects">
            <div ng-if="subtopic.objects">
            </div>
            </div ng-if="object">
            </div>
        </div>

        </div ng-if="object">
        </div>
    </div>

    <div ng-if="object">
    </div>
</div>

JSON.json

{
    subtopic: [
        {
            "title": "title 1",
            "subtitle": "title 2",
            "objects": [{
                "title": "123"
                "label": "456"
                "objects": [
                    {
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    },
                    {
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    }
                ]
            }]
        },
        {
            "title": "title 1",
            "subtitle": "title 2",
            "objects": [{
                "title": "123"
                "label": "456"
                "objects": [
                    {
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    },
                    {
                        "object": {
                            "object-1": "123",
                            "object-2": "456"
                        }
                    }
                ]
            }]
        },
    ]
}

0 个答案:

没有答案