I have a very simple Angular app - a nav bar, a sidebar and a main area.
I am using a data service to get the data for the cards in the main area.
Here is the code for the data service:
import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Observable } from "rxjs";
// Models
import { Card } from "../models/card";
import { Filter } from "../models/filter";
const httpOptions = {
headers: new HttpHeaders({ "Content-Type": "applicaiton/json" })
};
@Injectable({
providedIn: "root"
})
export class DataService {
getCardsUrl: string = "http://ip_address:3000/api/data/cards/";
getTimeframesUrl: string = "http://ip_address:3000/api/data/timeframe/";
constructor(private http: HttpClient) {}
getCardsData(): Observable<Card[]> {
return this.http.get<Card[]>(this.getCardsUrl);
}
getTimeframeFilters(): Observable<Filter[]> {
return this.http.get<Filter[]>(this.getTimeframesUrl);
}
}
I am subscribing to the data service inside the ngOnInit
lifecycle hook in main.component.ts.
Here is the code for main.component.ts
import { Component, OnInit } from "@angular/core";
import { DataService } from "../../services/data.service";
import { Card } from "../../models/card";
@Component({
selector: "app-main",
templateUrl: "./main.component.html",
styleUrls: ["./main.component.css"]
})
export class MainComponent implements OnInit {
cards: Card[];
loaded: boolean;
constructor(private dataService: DataService) {}
ngOnInit() {
this.loaded = false;
setTimeout(() => {
this.dataService.getCardsData().subscribe(cards => {
this.cards = cards;
this.loaded = true;
});
}, 2000);
}
}
Question: I want to update the contents of main based on the timeframe selection in the sidebar. How do I go about doing this? I am new to Angular, so maybe I am not understanding how Observables work. Can I please have some directions?