-1
I am studying angular and decided to make a very simple movie search. I would like to put the elements of the film on the screen, but the return of my get
has been an Object. I have broken my head to try to make it display. After reading a lot, I saw that the ngfor
cannot display the elements. Can someone tell me some way to convert the Object to array
. It could be one or the other solution of how to get the elements of this Object and display on the screen.
film-placehold.component.ts
import { Film } from './../models/films';
import { FilmsService } from './../services/films.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-film-placehold',
templateUrl: './film-placehold.component.html',
styleUrls: ['./film-placehold.component.css']
})
export class FilmPlaceholdComponent implements OnInit {
constructor(
private serviceFilms: FilmsService
) { }
ngOnInit() {
this.getFilms();
}
getFilms(){
this.serviceFilms.getFilmsService().subscribe(
(data) => {
//console.log(data);
this.serviceFilms.filmes = data //as Film[]
console.log(this.serviceFilms.filmes)
}
)
}
}
film.service.ts
import { Film } from './../models/films';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Observable} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class FilmsService {
url='https://www.omdbapi.com/?s=x-men&apikey=e03a9ac8';
constructor(
private httpClient:HttpClient
) { }
httpOptions = {
headers: new HttpHeaders({'Content-Type':'application/json'})
}
filmes:Film[]
getFilmsService(): Observable<any>{
return this.httpClient.get(this.url)
}
getSearchFilmeService(movieName){
return this.httpClient.get<Film[]>(`https://www.omdbapi.com/?s=${movieName}&apikey=e03a9ac8`)
}
}
filmplacehold.html
<div class="box">
<div class="container">
<li *ngFor="let filme of serviceFilms.filmes">
{{filme.Title}}
</li>
</div>
</div>
Wow, thank you for clarifying Andrade, it worked! :)
– falcon