我正在使用父对象对子对象进行分组。我在Plunker
上有一个简短的工作示例它工作正常,我理解在一个数组中嵌套对象的原理。现在我想使用两个不同的范围数组。 公司(父母) - 工人(孩子)的例子:
$scope.parent = [{id: 1, name: "Nike", productOf:"USA"}]
$scope.child = [{ id:1, firstName:"John", lastName:"Doe" },
{ id:2, firstName:"John2", lastName:"Doe2" },
{ id:3, firstName:"John3", lastName:"Doe3" }
]
我自己真的试图这样做,我读了很多文章,但我找不到我需要的东西。
有人可以给我一个提示做什么,或链接文章? 谢谢!
答案 0 :(得分:2)
我认为你的子范围需要一个引用父索引,比如
$scope.parent = [{id: 1, name: "Nike", productOf:"USA"},
{id: 2, name: "Fila", productOf:"CH"},
{id: 3, name: "BLA", productOf:"UK"}]
$scope.child = [{ id:1, firstName:"John", lastName:"Doe", pid:1},
{ id:2, firstName:"John2", lastName:"Doe2", pid:2 },
{ id:3, firstName:"John3", lastName:"Doe3", pid:3 }
答案 1 :(得分:1)
无论如何,你必须在workers数组中有映射字段到公司,然后你可以在ng-repeat中使用filter。
$scope.workers = [
{ id:1, companyId:1, firstName:"John", lastName:"Doe" },
{ id:2, companyId:2, firstName:"John2", lastName:"Doe2" },
{ id:3, companyId:3, firstName:"John3", lastName:"Doe3" },
HTML:
<div ng-repeat="worker in workers | filter:{companyId:company.id}">
请参阅Plunker
上的示例答案 2 :(得分:1)
根据您的Plunker:
<div class="container">
<div class="col-lg-6">
<h2 class="mainTitle">Click on group</h2>
</div>
<div ng-repeat="company in parentObj" class="header" ng-click="show=!show">
{{company.name}}-{{company.productOf}}
<div ng-show="show">
<div ng-repeat="worker in childObj" >
<p ng-if="worker.parent==company.id">{{worker.firstName}} {{worker.lastName}}</p>
</div>
</div>
</div>
angular.module('myApp',['angular.filter']) .controller('myCtrl',function($ scope,$ http){
$scope.parentObj = [
{id: 1, name: "Nike", productOf:"USA"},
{id: 2, name: "Fila", productOf:"CH"},
{id: 3, name: "BLA", productOf:"UK"}
]
$scope.childObj = [
{ id:1, firstName:"John", lastName:"Doe", parent:1},
{ id:2, firstName:"John2", lastName:"Doe2", parent:1 },
{ id:3, firstName:"John3", lastName:"Doe3", parent:1 },
{ id:1, firstName:"John4", lastName:"Doe4" , parent:2},
{ id:2, firstName:"John5", lastName:"Doe5", parent:2 },
{ id:3, firstName:"John6", lastName:"Doe6", parent:2 },
{ id:1, firstName:"John7", lastName:"Doe7", parent:3 },
{ id:2, firstName:"John8", lastName:"Doe8", parent:3 },
{ id:3, firstName:"John9", lastName:"Doe9", parent:3 }
]
});