After I call my API service, I have a undefined variable in the template, but I see the expected result in the console.
There is my VideoComponent that called API
export class VideoComponent implements OnInit {
video: Video = new Video();
constructor(private apiService: ApiService, private route: ActivatedRoute) {
}
ngOnInit() {
this.route.params.subscribe(params => {
this.apiService.getOneVideoByID(params['id']).subscribe((video: Video) => {
this.video = video;
console.log(this.video);
});
});
}
}
There is my API service:
export class ApiService {
PHP_API_SERVER = 'http://127.0.0.1/TutoWorld/backend/api';
private headers;
constructor(private httpClient: HttpClient) {
this.headers = new HttpHeaders().set('Content-Type', 'application/json')
}
getOneVideoByID(id: number): Observable<Video> {
return this.httpClient.get<Video>(`${this.PHP_API_SERVER}/readOne?id=${id}`,{headers: this.headers});
}
}
My template:
<div class="block5"></div>
<div id="videoContainer">
<p>video title: {{video.titre}} </p>
</div>
And my Video Model:
export class Video {
id: number;
url: string;
titre: string;
langue: string;
description: string;
date_creation: string;
date_ajout: string;
auteur_id: number;
auteur_nom: string;
}
If someone can help me to solve this issue, would be great.
Thanks
console.log(this.video)?<p>video: {{ video | json }} </p>. And do you seevideo title: ` present in the view?interfaceinstead of aclass. You are not using a constructor and interfaces don't get transpiled in javascript.