在ng-repeat中调用函数与普通函数相同。由于您需要在页面加载时显示子类别,因此最好事先获取这些数据。异步加载子类别将不适合这种情况。
这是实现此目的的简短代码段(JS Fiddle)
<div ng-app="app" ng-controller="ctrl"> <div ng-repeat="category in model.categories"> <span> Category: {{ category.name }} </span> <p ng-repeat="subCategory in getSubCategories(category.Id)">{{ subCategory.name }}</p> </div></div>
控制者
angular.module("app", []).controller('ctrl', ['$scope', function ($scope) {$scope.model = { categories: [{ "Id": 1, name: '1' }, { "Id": 2, name: '2' }], subCategories: [{ "parentId": 1, name: 'a1' }, { "parentId": 1, name: 'a2' }, { "parentId": 2, name: 'a3' }]}$scope.getSubCategories = function(parentId){ var result = []; for(var i = 0 ; i < $scope.model.subCategories.length ; i++){ if(parentId === $scope.model.subCategories[i].parentId){ result.push($scope.model.subCategories[i]); } } console.log(parentId) return result;}}])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)