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

151
Visualizações
How to display items on the page depending on the value of the field? Angular

I generate fields with loadFields(). All of them are successfully rendered in html in a row. But I need fields to be rendered separately depending on the group. To be able to group them and add a header. I tried doing it with *ngIf="item.group === 'group1'" but it doesn't work.

I was able to exclude a field before with *ngIf="item.key !== 'item1'"

loadFields() {
  this.fields = {
    item1: {
      group: 'group1',
      title: '',
      type: 'text',
      value: 'some value1',
      validation: [Validators.required]
    },
    item2: {
      group: 'group1',
      title: '',
      type: 'text',
      value: 'some value2',
      validation: [Validators.required]
    },
    item3: {
      group: 'group2',
      title: '',
      type: 'text',
      value: 'some value3',
      validation: [Validators.required]
    },
    item4: {
      group: 'group2',
      title: '',
      type: 'text',
      value: 'some value4',
      validation: [Validators.required]
    },
  }
}

originalOrder = (a: KeyValue<number, string>, b: KeyValue<number, string>): number => {
 return 0;
}
<ng-container *ngFor="let item of fields | keyvalue: originalOrder">

    <div class="col-lg-12">
      <legend class="h6">Group 1</legend>
      <div class="col-lg-12" *ngIf="item.group === 'group1'">
        <app-input-item
          [complexForm]="langComplexForm"
          [field]="item.value"
        ></app-input-item>
      </div>
    </div>

    <div class="col-lg-12">
      <legend class="h6">Group 2</legend>
      <div class="col-lg-12" *ngIf="item.group === 'group2'">
        <app-input-item
          [complexForm]="langComplexForm"
          [field]="item.value"
        ></app-input-item>
      </div>
    </div>

</ng-container>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you can use ngSwitch https://angular.io/api/common/NgSwitch something like this:

<ng-container 
  *ngFor="let item of fields | keyvalue: originalOrder"  
  >
<div [ngSwitch]="item.group"> 
  <div class="col-lg-12"  *ngSwitchCase="'group1'">
    <legend class="h6">Group 1</legend>
    <div class="col-lg-12">
      <app-input-item
        [complexForm]="langComplexForm"
        [field]="item.value"
      ></app-input-item>
    </div>
  </div>

  <div class="col-lg-12"  *ngSwitchCase="'group2'">
    <legend class="h6">Group 2</legend>
    <div class="col-lg-12">
      <app-input-item
        [complexForm]="langComplexForm"
        [field]="item.value"
      ></app-input-item>
    </div>
  </div>
</div>
</ng-container>
about 4 years ago · Juan Pablo Isaza 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