如何根据 Angularjs 中的特定 属性 显示 objects 的列表?
How do I display a list of objects based on a specific property in Angularjs?
我在 mongodb 中有一个一对多的相关集合模型,其中 child 集合中的文档包含 parent 集合中文档的 ID。我试图在查看 parent.
时显示所有 children
查看 parent 时,我有一个显示整个 child 集合的 ng-repeat。我正在努力过滤 child 集合,以便它只显示包含 parent id.
的结果
这是我的 HTML:
<div>{{parent.name}}</div>
<div ng-repeat="child in children | filter: {id: {{parent.id}}}">
{{child.name}}
</div>
这是我的 js:
app.controller("parentCtrl", function($scope, $location, $routeParams, $http) {
//Get parent
$scope.children = [];
$http.get('/children').success(function(children) {
$scope.loaded = true;
$scope.children = children;
});
});
代码成功获取 parent 并加载 child 集合中的所有文档。我可以在 ng-repeat 中使用过滤器来仅提取 children 吗?如果没有,是否有从相关集合中获取文档的最佳实践?
parent.id
不应该用插值 ({{}}
) 包装,因为它需要一个根据范围计算的表达式。
ng-repeat="child in children | filter: {id: parent.id }"
您可以使用此代码:
<div>{{parent.name}}</div>
<div ng-repeat="(k, child) in children | filter: {id: parent.id}"
{{child.name}}
</div>
我在 mongodb 中有一个一对多的相关集合模型,其中 child 集合中的文档包含 parent 集合中文档的 ID。我试图在查看 parent.
时显示所有 children查看 parent 时,我有一个显示整个 child 集合的 ng-repeat。我正在努力过滤 child 集合,以便它只显示包含 parent id.
的结果这是我的 HTML:
<div>{{parent.name}}</div>
<div ng-repeat="child in children | filter: {id: {{parent.id}}}">
{{child.name}}
</div>
这是我的 js:
app.controller("parentCtrl", function($scope, $location, $routeParams, $http) {
//Get parent
$scope.children = [];
$http.get('/children').success(function(children) {
$scope.loaded = true;
$scope.children = children;
});
});
代码成功获取 parent 并加载 child 集合中的所有文档。我可以在 ng-repeat 中使用过滤器来仅提取 children 吗?如果没有,是否有从相关集合中获取文档的最佳实践?
parent.id
不应该用插值 ({{}}
) 包装,因为它需要一个根据范围计算的表达式。
ng-repeat="child in children | filter: {id: parent.id }"
您可以使用此代码:
<div>{{parent.name}}</div>
<div ng-repeat="(k, child) in children | filter: {id: parent.id}"
{{child.name}}
</div>