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

273
Visualizações
How can I render two object in div?

I have 2 objects with data and I need to restruct the rendering in html.

This is what I get:

image

but this is what I need to obtain:

second

Black box is the *ngFor of articles object, respectively red one is *ngFor of adsContainer object

<div *ngFor="let article of articles; let i = index;">

<mat-card class="card">
  <div>
    <a class="title">
      {{article.title}}
    </a>
    <a>
      <p>{{article.content}}</p>
    </a>
  </div>

</mat-card>

<div class="container" *ngIf="(i % 7) === 6">
  <div *ngFor="let data of adsContainer">
    <div>
      <h1>{{data.value}}</h1>
    </div>
  </div>
</div>

</div>
  public adsContainer: any = [
    { id: '1', value: 'text value here' },
    { id: '2', value: 'text value here' },
    { id: '3', value: 'text value here' }
  ]
  public articles: any = [
    { id: '1', title: 'title value here', content: 'content here' },
    { id: '2', title: 'title value here', content: 'content here' },
    { id: '3', title: 'title value here', content: 'content here' },
    ........
    { id: '20', title: 'title value here', content: 'content here' },
  ]
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try to use directly the array , also when you call adsContainer[(parseInt(i / 6)-1)% adsContainer.length, it will always restart the index:

<div *ngFor="let article of articles; let i = index;">
    
  <mat-card class="card">
    <div>
      <a class="title">
        {{article.title}}
      </a>
      <a>
        <p>{{article.content}}</p>
      </a>
    </div> 
  
  </mat-card>
  
  <div class="container" *ngIf="(i % 7) === 6">
    <div>
      <div>
        <h1>{{adsContainer[(parseInt(i / 6)-1)%adsContainer.length].value}}</h1>
      </div>
    </div>
  </div>
  
</div>
about 4 years ago · Santiago Trujillo Relatório

0

Your issue is that you add your ads containers in a loop

<div class="container" *ngIf="(i % 7) === 6">
  <!-- this loop here -->
  <div *ngFor="let data of adsContainer">
    <div>
      <h1>{{data.value}}</h1>
    </div>
  </div>
</div>

what you want is to add only one ad container at a time, in order to do that, you have to rmove the loop

In order to have the three ads show, you'll have to do some math to get the ad index from the article index

something like this :

<div class="container" *ngIf="(i % 7) === 6">
  <div>
    <h1>{{adsContainer[mathFloor(i / 7)].value}}</h1>
  </div>
</div>

note that Math is not available in angular templates, you'll have to redefine floor as a component method

function mathFloor(val) {return Math.floor(val)}
about 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