【发布时间】:2019-11-22 09:48:57
【问题描述】:
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>
组件.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);
});
}
我正在尝试将活动类仅应用于轮播项目一次。我不能使用索引,因为它既不匹配结尾也不匹配第一个:
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
这使它显示它们彼此顶部而不是滑动... 我不相信有一种方法可以在 ngfor 中分配一个布尔值,因此不会再次向类添加活动
【问题讨论】:
-
尝试有条件地附加类,即 [ngClass]="{'active':
}" -
@Ininiv 这并不能解决问题,因为我的 JSON 来自 2 个单独的表,它们的关系是 postID。在我的解决方案中,我已经解决了在二维数组中迭代索引的问题:
fileList: (4) [Array(1), Array(2), Array(3), Array(2)] -
欢迎提出更好标题的建议 =)
标签: angular typescript multidimensional-array carousel ngfor