Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
How do I display a list of objects based on a specific property in Angularjs?

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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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 }"
over 4 years ago · Santiago Trujillo Relatório

0

You can use this code:

<div>{{parent.name}}</div>
<div ng-repeat="(k, child) in children | filter: {id: parent.id}"
    {{child.name}}
</div>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda