import {Injectable, OnInit} from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import {ActivatedRoute} from '@angular/router'; import {BehaviorSubject} from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ChampionshipService implements OnInit { protected httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }), withCredentials: true }; protected baseUrl = 'http://localhost:8080/'; private championships: any[] = []; /** * What's the current championship? Could be null if not loaded yet. */ private idChampionship = new BehaviorSubject(null); constructor(private http: HttpClient, private route: ActivatedRoute) { // load the champs from the DB this.loadChampionships(); } private loadChampionships() { this.http.get(`${this.baseUrl}championship`, this.httpOptions).subscribe(val => { console.log('loaded championships'); this.championships = val; console.log(val); }); } /** * Set the currently visible championship. * @param idChampionship */ setChampionship(idChampionship: number) { this.idChampionship.next(idChampionship); } getChampionships() { return this.championships; } getCurrentChampionship() { console.log(this.championships); const theOne = this.championships.filter(val => val.idchampionship === this.idChampionship.value); console.log(theOne); if (theOne && theOne.length > 0) { return theOne[0]; } else { return null; } } ngOnInit(): void { } }