请注意,以下内容有效,但感觉非常笨拙。我正在寻找使它更紧的任何技巧和/或我以后可能会后悔的陷阱。
嵌套对象的扁平数组:
$scope.parents =
[
{"name":"parent 1", "id":"1",
"children": [
{"name":"child 1a", "id":"1a", "parentid":"1"}, {"name":"child 1b", "id":"1b", "parentid":"1"}
]
},
{"name":"parent 2", "id":"2",
"children": [
{"name":"child 2a", "id":"2a", "parentid":"2"}, {"name":"child 2b", "id":"2b", "parentid":"2"}
]
}
];
需要填充到两个<select>
对象中,一个给父母,一个给孩子,并保持他们的层次关系。So that, when a parent is selected, children which are not underneath it are filtered out:
ng-options="item.id for item in children | filter:{'id': data.parents.id}"
as well as when a child is selected, the parent is automatically recognized across the model
$scope.parents = $filter('filter')($scope.parents, {id: $scope.data.children.parentid});
我觉得好像我错过了什么。