I need to create a carousel that shows pictures from a variable in the .ts. I planned using ngFor.
I have a static Bootstrap 4 carousel:
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="assets/fortress.jpg" class="d-block w-100 carousel-pic" alt="...">
</div>
<div class="carousel-item">
<img src="assets/fortress.jpg" class="d-block w-100 carousel-pic" alt="...">
</div>
</div>
</div>
And I have a variable in the .ts file called pics:
Now I tried putting the ngFor the carousel, and the active carousel just the first picture. Problem:
- The carousel are off. Just showing three. When I try setting the ngFor there, it doesn't work.
- Slide shows first picture (active) okay but the second slide show the same picture (because the loop starts again) and then it stops, doesn't show the third picture.
When I tried just setting the ngFor as carousel-item, it doesn't work because it requires active class. Also, the item?[0].pin_photos.url just gets the first picture.
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="http://MYURL/public/PinPhotos/{{item?.pin_photos[0].url}}"
class="d-block w-100 carousel-pic" alt="...">
</div>
<div *ngFor="let pic of pics">
<div class="carousel-item">
<img src="http://MYURL/public/PinPhotos/{{pic.url}}" class="d-block w-100 carousel-pic" alt="...">
</div>
</div>
</div>
</div>
What am I missing ?
I need to create a carousel that shows pictures from a variable in the .ts. I planned using ngFor.
I have a static Bootstrap 4 carousel:
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="assets/fortress.jpg" class="d-block w-100 carousel-pic" alt="...">
</div>
<div class="carousel-item">
<img src="assets/fortress.jpg" class="d-block w-100 carousel-pic" alt="...">
</div>
</div>
</div>
And I have a variable in the .ts file called pics:
Now I tried putting the ngFor the carousel, and the active carousel just the first picture. Problem:
- The carousel are off. Just showing three. When I try setting the ngFor there, it doesn't work.
- Slide shows first picture (active) okay but the second slide show the same picture (because the loop starts again) and then it stops, doesn't show the third picture.
When I tried just setting the ngFor as carousel-item, it doesn't work because it requires active class. Also, the item?[0].pin_photos.url just gets the first picture.
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="http://MYURL/public/PinPhotos/{{item?.pin_photos[0].url}}"
class="d-block w-100 carousel-pic" alt="...">
</div>
<div *ngFor="let pic of pics">
<div class="carousel-item">
<img src="http://MYURL/public/PinPhotos/{{pic.url}}" class="d-block w-100 carousel-pic" alt="...">
</div>
</div>
</div>
</div>
What am I missing ?
Share Improve this question asked Jan 12, 2020 at 11:28 IkePrIkePr 9305 gold badges19 silver badges48 bronze badges5 Answers
Reset to default 4I use ngFor
first variable to set the class for the first item
template
<div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleCaptions" *ngFor="let item of items;let index = index" [attr.data-slide-to]="index"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item" *ngFor="let item of items;let index = index;let isFirst = first" [ngClass]="{active:isFirst}">
<img [src]="item.url" class="d-block w-100" [alt]="item.title">
<div class="carousel-caption d-none d-md-block">
<h5 [textContent]="item.title"></h5>
<p [textContent]="item.symmery"></p>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
trigger carousel manualy like this
ngAfterViewInit(){
$('#carouselExampleCaptions').carousel()
}
demo