ANGULAR: Can't get my data to show on my HTML file

Viewed 693

I am fetching the api json placeholder and i am trying to get a page matching with the id of the route en the object. By console logging everything seems to work, when i navigate to the specific page, the object with the matching id shows up on my console but when i try to put the data on my html it just won't show up. And i get no errors. I don't know what to do. screenshot of the page

//SERVICE FILE//
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Stream } from './stream';
import { Observable} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class StreamService{

constructor(private http: HttpClient) { }

getStream():Observable<Stream[]>{
  return this.http.get<Stream[]>("https://jsonplaceholder.typicode.com/albums/1/photos");
  }

getLiveStream(id: number): Observable<Stream> {
  const url = `https://jsonplaceholder.typicode.com/albums/1/photos?id=${id}`;
  return this.http.get<Stream>(url);
  }
}
//TS FILE//
import { Component, OnInit } from '@angular/core';
import { StreamService } from '../stream.service';
import { DomSanitizer } from '@angular/platform-browser';
import {ActivatedRoute} from '@angular/router';
import { Stream } from '../stream';

@Component({
  selector: 'app-livestream',
  templateUrl: './livestream.component.html',
  styleUrls: ['./livestream.component.scss']
})
export class LivestreamComponent implements OnInit {
  liveStream!: Stream;

  constructor(
    private streamService: StreamService, 
    private sanitizer: DomSanitizer, 
    private route: ActivatedRoute, 
    ) { }

    //is voor de "sanitizing unsafe style value url" error te vermijden//
    public getSantizeUrl(url : string) {
      return this.sanitizer.bypassSecurityTrustUrl(url);
  }

  ngOnInit() { 
    this.getLiveStream();

  }
      getLiveStream():void{
        const id = Number(this.route.snapshot.paramMap.get('id'));
        this.streamService.getLiveStream(id).subscribe(liveStream =>{
            this.liveStream = liveStream;
            console.log(this.liveStream);
        })
      }


    
    
}
//INTERFACE FILE
export interface Stream {
  id: number;
  title: string;
  url: string;
  thumbnailUrl: string;
}
// HTML FILE
<div class="container">
  <h3>Livestream</h3>
  <div>
     <h4> {{liveStream?.title}} </h4>
  </div>
</div>
3 Answers

As established in other answer, what you receive is an array with one object. I would extract the object from the array and also like to use the async pipe. So what I would do is:

import { map } from 'rxjs/operators';

//....

getLiveStream(id: number): Observable<Stream> {
  const url = `https://jsonplaceholder.typicode.com/albums/1/photos?id=${id}`;
  return this.http.get<Stream[]>(url).pipe(
    map((streams: Stream[]) => streams => streams[0] || {} as Stream)
  );
}

Component:

import { Observable} from 'rxjs';

// ...

liveStream$: <Observable>Stream;

getLiveStream():void{
  const id = Number(this.route.snapshot.paramMap.get('id'));
  this.liveStream$ = this.streamService.getLiveStream(id);

Template:

 <div *ngIf="liveStream$ | async as liveStream">
   <h4> {{liveStream.title}} </h4>
 </div>

you can't call {{liveStream?.title}} directly because the api returns an array of livestream. You can do {{liveStream['title']}} however not the best approach. I would suggest using ng-for

<div class="container">
  <h3>Livestream</h3>
  <div *ngFor="let stream of liveStream">
     <h4> {{stream.title}} </h4>
  </div>
</div>

you can find more example in angular docs

As other answers pointed out the problem, it was because i get an array and wasn't looping over it, so i just assigned array brackets to my observable types in my service file and component file, looped over it and it works now.

//TS file //
export class LivestreamComponent implements OnInit {
liveStream!: Stream[];
// SERVICE FILE //
getLiveStream(id:number):Observable<Stream[]> {
  const url = `https://jsonplaceholder.typicode.com/albums/1/photos?id=${id}`;
  return this.http.get<Stream[]>(url);
  }
<div class="container">
  <h3>Livestream</h3>
  <div *ngFor="let liveStream of liveStream">
    <h4> {{liveStream.id}} </h4>
     <h4> {{liveStream.title}} </h4>
     <img src="{{liveStream.url}}">
  </div>
</div>
Related