javascript - 用 Angular 显示json结果

标签 javascript json angular

我正在尝试从网络 API 获取一些 json 数据。到目前为止,我已经收到来自 api 的响应,现在我正在尝试定位一个值并将该值插入到 youtube 链接的末尾。

movie.component.html

<div *ngIf="movie">
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">{{movie.title}}</h3>
    </div>
    <div class="panel-body" >
        <div class="row">
            <div class="col-md-7">
                <img class="thumbnail" src="http://image.tmdb.org/t/p/w500/{{movie.poster_path}}">
            </div>
            <div class="col-md-5">
                <ul class="list-group">
                    <li class="list-group-item">Genres:<span *ngFor="let genre of movie.genres"> {{genre.name}}</span></li>
                    <li class="list-group-rel">Release Date: {{movie.release_date | date}}</li>
                </ul>
                <p>{{movie.overview}}</p>
                <br>
                **<div *ngIf="videos">
                 <div *ngFor="let video of videos">
                 <iframe width="360" height="215" src="https://www.youtube.com/embed/{{video.key}}" frameborder="0" allowfullscreen></iframe>**
                </div>
                </div>
                <h4>Rating</h4>
                <p>{{movie.vote_average}} / 10</p>
                <a *ngIf="movie.homepage" href="{{movie.homepage}}" target="_blank" class="btn btn-default">Visit Movie Website</a>
            </div>
        </div>
    </div>
</div>
</div>

-----------------
movie.component.ts

export class MovieComponent implements OnInit {


movie:Object;
videos: Object;

constructor(private router:ActivatedRoute, private _movieService:MovieService) 
{ 

}

ngOnInit() {

this.router.params.subscribe((params) =>{
    let id = params['id'];
    this._movieService.getMovie(id).subscribe(movie =>{
        this.movie = movie;
  });
});
   this.router.params.subscribe((params) =>{
  let id = params['id'];
  this._movieService.getTrailer(id).subscribe(videos =>{
    this.videos = videos;
    });
});
}

}

这是来自 Api 的响应,我正在尝试定位“键”值并将其插入到 youtube 链接的末尾 this is the response from Api, I'm trying to target the "key" value and insert it at the end of the youtube link

但是我得到这个错误 but i get this error

最佳答案

这里的问题是您将 *ngFor 用于对象。

*ngFor: is used to iterate arrays.

现在你得到的响应是一个对象,在这个对象中你有一个数组result我相信这是你想要迭代的数组。

您只需要更改模板中的 *ngFor。

...
 <div *ngFor="let video of videos.results">
...

关于javascript - 用 Angular 显示json结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45580116/

相关文章:

javascript - 未调用 AJAX 回调

javascript - e.keyCode 不起作用

JAVA ObjectNode 获取JSON数组

javascript - Angular2 - 获取 DOM 中的特定元素并更改其类

javascript - 从 CSS 中删除包含对外部文件的引用的行

javascript - ES6 类中的嵌套方法?

javascript - 准确的长轮询示例?

javascript - 在 JavaScript 中读取 JSON 文件

javascript - Angular2 - 选中 ngFor 和 react 形式中的复选框

javascript - JSObjects如何动态访问 child