使用 ngFor 和 ngIf 显示所有带有索引的一件事

发布于 2025-01-11 01:55:46 字数 1395 浏览 2 评论 0原文

当我点击“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2025-01-18 01:55:46

this.showMovies = dataFromService, 之后向您的 showMovies 对象添加新属性:

this.showMovies.forEach( movie => {
    movie['showMoreInfo'] = false;    
});

over 方法中替换 this.showMoreInfo = true 为:

this.showMovies[i].showMoreInfo = true;

leave 方法中,将 this.showMoreInfo = false 替换为:

this.showMovies[i].showMoreInfo = false;

然后在模板中执行:

*ngIf="m.showMoreInfo"

并替换

with:

和添加trackByFn 到 ts:

trackByFn(index) {
    return index;
}

编辑:您可能需要更改 IData 类型以包含“showMoreInfo”

Add new property to your showMovies object after this.showMovies = dataFromService,:

this.showMovies.forEach( movie => {
    movie['showMoreInfo'] = false;    
});

In over method replace this.showMoreInfo = true with:

this.showMovies[i].showMoreInfo = true;

In leave method replace this.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:

trackByFn(index) {
    return index;
}

EDIT: you might need to change your IData type to include 'showMoreInfo'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文