Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda