I have a one to many model of related collections in mongodb where a doc in the child collection includes the id of a doc in the parent collection. I'm trying to display all children when viewing the parent.
When viewing the parent, I have an ng-repeat that displays the entire child collection. I'm struggling with filtering the child collection so that it only displays results that include the parent id.
Here's my HTML:
<div>{{parent.name}}</div>
<div ng-repeat="child in children | filter: {id: {{parent.id}}}">
{{child.name}}
</div>
Here's my 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;
});
});
The code successfully gets the parent and loads all docs in the child collection. Can I use a filter in my ng-repeat to pull just the children? If not, is there a best practice to get docs from a related collection?
parent.id shouldn't be wrap with interpolation({{}}) as it needs an expression that evaluates against scope.
ng-repeat="child in children | filter: {id: parent.id }"
You can use this code:
<div>{{parent.name}}</div>
<div ng-repeat="(k, child) in children | filter: {id: parent.id}"
{{child.name}}
</div>