Angular 12 - 找不到不同的支持对象“[object Object]”类型为“对象”。 NgFor 仅支持绑定到 Iterables,例如数组

发布于 2025-01-15 05:57:00 字数 960 浏览 3 评论 0原文

我从外部 API 获取了大量数据,我想在我的 html 中实现它,但是每次我尝试对数据执行 *ngFor 时,它最终都会出错,有人可以告诉我如何解决这个问题??

 --------HTML-----------

<ng-container *ngFor="let movie of filme">
<mat-card class="mat-card" fxFlex="0 1 calc(33.3% - 11px)"
fxFlex.lt-md="0 1 calc(50% - 11px)"
fxFlex.lt-sm="100%">
  <mat-card-header >
  <mat-card-title class="mat-card-title">
    <div>
      <p>{{movie}}</p>
    </div>
  </mat-card-title>
  </mat-card-header>
</mat-card>
</ng-container>

---------TYPESCRIPT------

  filme:any[];;

 imgUrl: string;
 titleImg: string;

  constructor(private filmes: BuscarFilmesService) {
      this.filmes.filmes().subscribe(res=>this.filme=res);
  }

 -------SERVICE----

  filmes(): Observable<Filmes[]> {
    return this.htpp.get<Filmes[]>(`${this.API_Url}/discover/movie?sort_by=popularity.desc&api_key=${this.API_key}&language=pt-BR`);
  }

I'm getting a lot of data from an external API, I want to implement it in my html, but every time I try to do a *ngFor with the data, it ends up going wrong, Could someone tell me how to solve this??

 --------HTML-----------

<ng-container *ngFor="let movie of filme">
<mat-card class="mat-card" fxFlex="0 1 calc(33.3% - 11px)"
fxFlex.lt-md="0 1 calc(50% - 11px)"
fxFlex.lt-sm="100%">
  <mat-card-header >
  <mat-card-title class="mat-card-title">
    <div>
      <p>{{movie}}</p>
    </div>
  </mat-card-title>
  </mat-card-header>
</mat-card>
</ng-container>

---------TYPESCRIPT------

  filme:any[];;

 imgUrl: string;
 titleImg: string;

  constructor(private filmes: BuscarFilmesService) {
      this.filmes.filmes().subscribe(res=>this.filme=res);
  }

 -------SERVICE----

  filmes(): Observable<Filmes[]> {
    return this.htpp.get<Filmes[]>(`${this.API_Url}/discover/movie?sort_by=popularity.desc&api_key=${this.API_key}&language=pt-BR`);
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

滴情不沾 2025-01-22 05:57:00

您的代码片段看起来是正确的。我的猜测是订阅中的 res 返回一个对象而不是数组。

我首先尝试通过执行以下操作来查看 res 是否实际上是一个数组:

constructor(private filmes: BuscarFilmesService) {

   this.filmes.filmes().subscribe(res =>
   {   
       this.filme = res;
       console.log(this.filme);
   });
}

如果它确实返回一个数组。然后,您可以尝试使用简单的 div 和 *ngFor 来缩小范围,以防 HTML 出现一些错误。

<div *ngFor="let movie of filme">
  {{movie}}
</div>

Your code fragments look right. My guess is that res in the subscription return an object instead of an array.

I would first try and see if the res is actually an array by doing this:

constructor(private filmes: BuscarFilmesService) {

   this.filmes.filmes().subscribe(res =>
   {   
       this.filme = res;
       console.log(this.filme);
   });
}

If it does return an array. Then you can try narrow it down by using simple div with the *ngFor just in case there are some errors with the HTML.

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