【问题标题】:Angular - Displaying field from TMDB apiAngular - 显示来自 TMDB api 的字段
【发布时间】:2019-08-12 11:24:29
【问题描述】:

尝试显示来自 TMDB 电影 api 的名为“认证”的字段,我成功获取所有其他信息,但不适用于该特定字段:

电影组件代码:

import { Component, OnInit } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import {MoviesService} from '../movies.service';

@Component({
  selector: 'app-movie',
  templateUrl: './movie.component.html',
  styleUrls: ['./movie.component.css']
})
export class MovieComponent implements OnInit {
  movie: Object;
  reviews: Array<Object>;
  similarMovies: Array<Object>;
  certification: Object;
  cast: Array<Object>;
  video: Object;
  constructor(
    private _moviesServices: MoviesService,
    private router: ActivatedRoute,
    private sanitizer: DomSanitizer
    ) {

  }

  ngOnInit() {
    this.router.params.subscribe((params) => {
      const id = params['id'];
      this._moviesServices.getMovie(id).subscribe(movie => {
        this.movie = movie;
      });
      this._moviesServices.getMovieReviews(id).subscribe((res: any) => {
        this.reviews = res.results;
      });
      this._moviesServices.getMovieCredits(id).subscribe((res: any) => {
        res.cast = res.cast.filter((item) => {return item.profile_path});
        this.cast = res.cast.slice(0,8);
      });
      this._moviesServices.getMovieVideos(id).subscribe((res: any) => {
        if(res.results && res.results.length) {
          this.video = res.results[0];        
          this.video['url'] = this.sanitizer.bypassSecurityTrustResourceUrl('https://www.youtube.com/embed/' + this.video['key']);
        }
      });
      this._moviesServices.getSimilarMovies(id).subscribe((res: any) => {
        console.log(res.results);
        this.similarMovies = res.results.slice(0, 18);
      });
      this._moviesServices.getCertification(id).subscribe((res: any) => {
        console.log(res.results);
        this.certification = res.results;
      });
    })
  }

}

这不起作用:

this._moviesServices.getCertification(id).subscribe((res: any) => {
        console.log(res.results);
        this.certification = res.results;
      });

html代码:

<p class="badge badge-pill badge-success p-2" *ngIf="movie.certification">
        {{movie.certification }} 
      </p>

电影服务:

getCertification(id: string) : Observable<any>  {
    const params = new HttpParams()
    .set('api_key', this.apikey);
    return this.http.get('https://api.themoviedb.org/3/movie/'+ id +'/release_dates', {params})

  }

API 网址:https://api.themoviedb.org/3/movie/99/release_dates?api_key="my_api_key"

【问题讨论】:

  • 能否显示组件代码
  • @Jamie,是的,我已经用共同代码更新了问题
  • 你能做stackblits吗
  • 谢谢,控制台有错误吗?因为movie.certification 将是一个对象数组,所以要将它放在 UI 上,您需要遍历数组。
  • 谢谢!这是它在控制台中显示的内容: 25: {iso_3166_1: "US", release_dates: 'Array(2)} 和 release_dates: Array(2) 0: {certification: "PG-13", iso_639_1: "en", note : "Dolby Theater", release_date: "2019-07-13T00:00:00.000Z", type: 1} ' 这就是我想要展示的认证

标签: angular themoviedb-api


【解决方案1】:

根据 API 文档,发布日期端点返回每个国家/地区的发布日期集合,并且每个发布日期都有一个认证属性。

所以你真的应该做到以下几点:

在您的组件中,将您的 certification 属性更改为 Array&lt;Object&gt;

然后在您的 HTML 中,您将要循环遍历数组,然后循环遍历 release_dates 数组,因为每个发布日期都有一个认证。

下面的代码我不知道它是否能正确编译,但它应该给你一个想法。

<p class="badge badge-pill badge-success p-2" *ngIf="certification">
    <span *ngFor="let cert of certification">
        <span *ngFor="let release of cert.release_dates"> 
            <span *ngIf="release.certification">{{release.certification}}</span>
        </span>
    </span>
</p>

以上可能不符合您的实际要求,例如如果您只想要美国的认证,那么您可能应该在收到数据时过滤掉组件中的其余数据,例如

this._moviesServices.getCertification(id).subscribe((res: any) => {
    const usCertifications = res.results.filter(result => {
        return result.iso_3166_1 === "US";
    };
    this.certification = usCertifications;
});

【讨论】:

  • @杰米谢谢!我现在理解并感谢 html,现在我在控制台中作为数组对象获取,但只是缺少一些东西来在 ui 端显示它们。你觉得我的服务好吗?这是控制台结果: (3) [ {...}, {...}, {...}] 0: {iso_3166_1: "HU", release_dates: Array(1)} 1: {iso_3166_1: "AU", release_dates: Array (1)} 2: {iso_3166_1: "IE", release_dates: Array(1)} 3: {iso_3166_1: "US", release_dates: Array(1)}
  • 服务很好,它正在获取数据,你只需要遍历数组。我已经更新了我的示例,但您可以看到您需要循环通过 certification,然后通过 release_dates 以获取该发布日期的 certification
  • @Jamie,非常感谢您的及时帮助,现已修复。真的很感激。 :) 仍在尝试了解从 Angular js 到 Angular latest 的迁移。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多