
JS
html<!DOCTYPE html><html ng-app="myApp"><head> <title>Angular JS Tree Rendering</title> <script src="https://Ajax.Googleapis.com/Ajax/libs/AngularJS/1.8.2/angular.min.JS"></script></head><body ng-controller="myCtrl"> <h1>JSON Data in Tree Format</h1> <ul> <li ng-repeat="item in data"> {{item.name}} <ul> <li ng-repeat="child in item.children"> {{child.name}} <ul ng-if="child.children.length > 0"> <li ng-repeat="grandchild in child.children"> {{grandchild.name}} </li> </ul> </li> </ul> </li> </ul></body></html>JavaScript代码:Javascriptangular.module('myApp', []).controller('myCtrl', function($scope) { $scope.data = [ { name: 'Parent 1', children: [ { name: 'Child 1.1', children: [] }, { name: 'Child 1.2', children: [ { name: 'Grandchild 1.2.1', children: [] }, { name: 'Grandchild 1.2.2', children: [] } ] } ] }, { name: 'Parent 2', children: [ { name: 'Child 2.1', children: [] } ] } ];});在以上代码中,我们创建了一个Angular JS应用,并定义了一个控制器。控制器中定义了一个名为"data"的JSON对象,其中包含了树状结构的数据。在HTML页面中,我们使用ng-repeat指令遍历JSON对象的属性,并将其渲染到页面上。通过嵌套使用ng-repeat指令,我们实现了树状结构的展示效果。本文介绍了如何使用Angular JS以树状格式渲染JSON数据。通过遵循上述步骤,并使用ng-repeat指令,我们可以将复杂的JSON数据以可视化的方式展示出来。希望本文对你理解Angular JS的数据绑定和视图渲染有所帮助。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号