0

I made an array called title in my component.ts file:

    this.projectInfo = {
      title: [
        'Title 1',
        'Title 2',
        'Title 3',
        'Title 4',
        'Title 5',
        'Title 6',
      ], 

And I want to use only one of the titles in that array for each "project" in my template:

  <div class="item2">
    <img class="center" src="https://natureconservancy-h.assetsadobe.com/is/image/content/dam/tnc/nature/en/photos/tnc_48980557.jpg?crop=961,0,1928,2571&wid=600&hei=800&scl=3.21375">
    <h5 *ngFor="let projectTitle of projectInfo.title">{{projectTitle}}</h5>
    <p>{{ projectInfo.description }}</p>
  </div>
  <div class="item2">
    <img class="center" src="https://natureconservancy-h.assetsadobe.com/is/image/content/dam/tnc/nature/en/photos/tnc_48980557.jpg?crop=961,0,1928,2571&wid=600&hei=800&scl=3.21375">
    <h5 *ngFor="let projectTitle of projectInfo.title">{{projectTitle}}</h5>
    <p >{{ projectInfo.description }}</p>
  </div>
  <div class="item2">
    <img class="center" src="https://natureconservancy-h.assetsadobe.com/is/image/content/dam/tnc/nature/en/photos/tnc_48980557.jpg?crop=961,0,1928,2571&wid=600&hei=800&scl=3.21375">
    <h5 *ngFor="let projectTitle of projectInfo.title">{{projectTitle}}</h5>
    <p>{{ projectInfo.description }}</p>
  </div>

I want to use "Title 1" for the first "project", "Title 2" for the second "project" and so on...

I've tried using let i = index to help specify which title from that array I want to use but couldn't find out how to do it.

I'm still pretty new to Angular, so any help/guidance will be very much appreciated. Thanks!

R. Richards
  • 24,603
  • 10
  • 64
  • 64

2 Answers2

0

What about:

{{projectTitle[0]}}

?

Replace 0 with i. More here.

Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41
0

You put it on the whole block and repeat the block

<div class="item2" *ngFor="let projectTitle of projectInfo.title">
  <img class="center" src="https://natureconservancy-h.assetsadobe.com/is/image/content/dam/tnc/nature/en/photos/tnc_48980557.jpg?crop=961,0,1928,2571&wid=600&hei=800&scl=3.21375">
  <h5>{{projectTitle}}</h5>
  <p>{{ projectInfo.description }}</p>
</div>
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60