我正在创建一个“树”导航,并且在嵌套子项时遇到问题,服务器向我发送如下 JSON 数据:
JSON:
0: {id:5, parentid:0, text:PlaceHolder}
1: {id:6, parentid:0, text:PlaceHolder}
2: {id:7, parentid:0, text:PlaceHolder}
3: {id:8, parentid:7, text:PlaceHolder}
4: {id:9, parentid:7, text:PlaceHolder}
5: {id:10, parentid:5, text:PlaceHolder}
6: {id:11, parentid:5, text:PlaceHolder}
7: {id:12, parentid:7, text:PlaceHolder}
8: {id:15, parentid:0, text:PlaceHolder}
9: {id:16, parentid:15, text:PlaceHolder}
10: {id:17, parentid:5, text:PlaceHolder}
11: {id:18, parentid:15, text:PlaceHolder}
12: {id:19, parentid:15, text:PlaceHolder}
13: {id:20, parentid:0, text:PlaceHolder}
14: {id:21, parentid:15, text:PlaceHolder}
15: {id:23, parentid:5, text:PlaceHolder}
注意'parentid'。“0”表示该项目是顶级父级。例如,如果 'parentid' 是 7,那么它将是 'id' 7 的孩子。
HTML:
<li ng-repeat="link in links"><a href="#/page/{[{link.id}]}">{[{link.text}]}</li>
JS:
var Site = angular.module('Site', []);
Site.config(function ($routeProvider) {
$routeProvider
.when('/page/:slug', {templateUrl: 'partials/page.html', controller: 'RouteController'})
.otherwise({redirectTo: '/page/home'});
});
function AppController ($scope, $rootScope, $http) {
/* Load pages on startup
$http.get('pages.json').success(function (data) {
$rootScope.pages = data;
}); */
// Load navigation on startup
$http.get('/university/list').success(function (data) {
$rootScope.links = data;
});
// Set the slug for menu active class
$scope.$on('routeLoaded', function (event, args) {
$scope.slug = args.slug;
});
}
function RouteController ($scope, $rootScope, $routeParams) {
// Getting the slug from $routeParams
var slug = $routeParams.slug;
$scope.$emit('routeLoaded', {slug: slug});
$scope.page = $rootScope.pages[slug];
}