0

In my project I have a product page and i display the product item's photos in a bootstrap carousel using Angular. What i want is to display at the top of the carousel the number of item's images like that: 1 of 18 images and when someone clicks the carousel's next button to change it to 2 of 18 images, 3 of 18 images etc.

I used .length to get/print the number of the total images of the product item eg:

{{ productItem.photos.length }}

so it displays now: 18 images But how can i make it to display 1 of 18 images?

I am new in Angular

Designer
  • 875
  • 7
  • 26
  • https://stackoverflow.com/questions/35405618/ngfor-with-index-as-value-in-attribute there is a index functionality in ngFor loop, you can use it – Nambi N Rajan Apr 04 '22 at 11:13

3 Answers3

0

Use index on *ngFor. On each loop iteration index is increased by one, like it would be on a normal for loop.

<div *ngFor="let image of images; index as i">
index is: {{i}}
</div>
Joosep Parts
  • 5,372
  • 2
  • 8
  • 33
0
<div *ngFor = "let image of product.images, index as i">
  {{i}} of {{product.images.length}}
</div>
Mox Shah
  • 13
  • 6
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Apr 04 '22 at 14:46
  • Okay will keep this in mind – Mox Shah Apr 06 '22 at 06:50
0

The ngForOf directive is generally used in the shorthand form ngFor. In this form, the template to be rendered for each iteration is the content of an anchor element containing the directive.

productItem.component.ts

import { Component} from '@angular/core';

@Component({
    selector: 'my-productItem',
    templateUrl: './productItem.component.html',
    styleUrls: []
})
export class AppComponent
{
    productItem= ["product1", "product2", "product3","product4"];
}

productItem.component.html

<li *ngFor="let item of productItem; let i = index">
      {{i+1}} of {{productItem.length}}
</li>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197