使用 ngFor 和 ngIf 显示所有带有索引的一件事
当我点击“mer info”(瑞典语)时,我在 console.log 中得到正确的索引号。但在网站上一切都显示出来了。关闭时也一样。我错过了什么?该信息来自 api,使用 observalbes 和 subscribe
<section>
<article *ngFor="let m of showMovies; let i =index" >
<p (click)="over(i)">Mer info</p>
<img [src]="m.imageUrl">
<div class="containmovie">
<p class="moviename">Movie:</p>
<p>{{ m.name}}</p>
<div class="showmore" *ngIf="showMoreInfo">
<button (click)="leave(i)">X</button>
{{ m.description }}
År: {{ m.year }}
</div>
</div>
</article>
</section>
showMovies: IData[] = []
order: IData[] = [];
checkOutId: number = 0;
showMoreInfo: boolean = false
toCheckout: number = 0;
constructor(private service: MoviesService, private route: ActivatedRoute) { }
ngOnInit(): void {
this.service.movies$.pipe(take(1)).subscribe((dataFromService: IData[]) => {
this.showMovies = dataFromService,
console.log(dataFromService)
})
this.service.getServerData()
}
over(i: number) {
this.showMoreInfo = true
console.log(i)
}
leave(i: number) {
this.showMoreInfo = false
console.log(i)
}
when i click on the "mer info" (swedish) i get the right index number in console.log. But on the website everything is showing. same when closing. What have i missed? the information is from an api, using observalbes and subscribe
<section>
<article *ngFor="let m of showMovies; let i =index" >
<p (click)="over(i)">Mer info</p>
<img [src]="m.imageUrl">
<div class="containmovie">
<p class="moviename">Movie:</p>
<p>{{ m.name}}</p>
<div class="showmore" *ngIf="showMoreInfo">
<button (click)="leave(i)">X</button>
{{ m.description }}
År: {{ m.year }}
</div>
</div>
</article>
</section>
showMovies: IData[] = []
order: IData[] = [];
checkOutId: number = 0;
showMoreInfo: boolean = false
toCheckout: number = 0;
constructor(private service: MoviesService, private route: ActivatedRoute) { }
ngOnInit(): void {
this.service.movies$.pipe(take(1)).subscribe((dataFromService: IData[]) => {
this.showMovies = dataFromService,
console.log(dataFromService)
})
this.service.getServerData()
}
over(i: number) {
this.showMoreInfo = true
console.log(i)
}
leave(i: number) {
this.showMoreInfo = false
console.log(i)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
this.showMovies = dataFromService,
之后向您的showMovies
对象添加新属性:在
over
方法中替换this.showMoreInfo = true 为:
this.showMovies[i].showMoreInfo = true;
在
leave
方法中,将this.showMoreInfo = false
替换为:this.showMovies[i].showMoreInfo = false;
然后在模板中执行:
*ngIf="m.showMoreInfo"
并替换
with:
和添加trackByFn 到 ts:
编辑:您可能需要更改 IData 类型以包含“showMoreInfo”
Add new property to your
showMovies
object afterthis.showMovies = dataFromService,
:In
over
method replacethis.showMoreInfo = true
with:this.showMovies[i].showMoreInfo = true;
In
leave
method replacethis.showMoreInfo = false
with:this.showMovies[i].showMoreInfo = false;
Then in your template do:
*ngIf="m.showMoreInfo"
and replace
<article *ngFor="let m of showMovies; let i =index" >
with:<article *ngFor="let m of showMovies; let i = index; trackBy: trackByFn">
and add trackByFn to ts:
EDIT: you might need to change your IData type to include 'showMoreInfo'