component.html
<section *ngFor="let project of projectList" class="child p-5">
<div class="first">
<h1>{{ project.title }}</h1>
<p><strong>{{ project.description }}</strong></p>
</div>
<div id="car_{{ project.title}}" class="carousel slide second" data-ride="carousel">
<div class="carousel-inner">
<div *ngFor="let file of mediaList">
<div *ngIf="file.postId == project._id ">
<div class="carousel-item active">
<img src="{{ url+file.path }}" class="d-block mx-auto"
style="height: -webkit-fill-available;">
</div>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#car_{{ project.title}}" 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="#car_{{ project.title}}" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</section>
component.ts
ngOnInit() {
this.url = environment.backendBaseUrl;
this.posts.getPost().subscribe(
res => {
this.projectList = res['posts'];
console.log(res['posts']);
},
err => {
console.log(err);
});
this.media.getMedia().subscribe(
res => {
this.mediaList = res['mediaFiles'];
console.log(res['mediaFiles']);
},
err => {
console.log(err);
});
}
I am struggling with ensuring the carousel item only has the 'active' class applied once. It seems impossible to achieve this using the ngFor index as it won't match either beginning or end:
0: {_id: "5dd78c7ff21808b10c9273dd", title: "SingleImage", description: "single image↵Nam quis tortor nec ligula auctor rut…at tellus vitae maximus. Donec in efficitur odio."}
1: {_id: "5dd78cf3f21808b10c9273df", title: "MultipleImages", description: "Multiple↵Nam quis tortor nec ligula auctor rutrum.…at tellus vitae maximus. Donec in efficitur odio."}
2: {_id: "5dd78ee4cbf2ecadd8e2ac53", title: "Mixed", description: "Mixed↵Nam quis tortor nec ligula auctor rutrum. Cu…at tellus vitae maximus. Donec in efficitur odio."}
0: {_id: "5dd78c7ff21808b10c9273de", path: "uploads\admin\1574407295057.png", type: "image/png", postId: "5dd78c7ff21808b10c9273dd", __v: 0}
1: {_id: "5dd78cf3f21808b10c9273e0", path: "uploads\admin\1574407411823.png", type: "image/png", postId: "5dd78cf3f21808b10c9273df", __v: 0}
2: {_id: "5dd78cf3f21808b10c9273e1", path: "uploads\admin\1574407411821.png", type: "image/png", postId: "5dd78cf3f21808b10c9273df", __v: 0}
3: {_id: "5dd78ee4cbf2ecadd8e2ac54", path: "uploads\admin\1574407908979.png", type: "image/png", postId: "5dd78ee4cbf2ecadd8e2ac53", __v: 0}
4: {_id: "5dd78ee5cbf2ecadd8e2ac55", path: "uploads\admin\1574407908975.png", type: "image/png", postId: "5dd78ee4cbf2ecadd8e2ac53", __v: 0}
5: {_id: "5dd78ee5cbf2ecadd8e2ac56", path: "uploads\admin\1574407908967.mp4.gif", type: "video/mp4", postId: "5dd78ee4cbf2ecadd8e2ac53", __v: 0}
length: 6
This arrangement causes the items to stack on top of each other rather than sliding properly... I don't believe there is a straightforward way to assign a boolean inside the ngFor loop to avoid adding the 'active' class multiple times.