Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!