MCPcopy Create free account
hub / github.com/DeborahK/Angular-Signals / AppComponent

Class AppComponent

cartApp/src/app/app.component.ts:24–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22 styleUrl: './app.component.css'
23})
24export class AppComponent {
25 title = 'cartApp';
26
27 // This would be in a service
28 private vehicleUrl = 'https://swapi.py4e.com/api/vehicles';
29
30 // Signals to support the template
31 quantity = signal(1);
32 selectedVehicle = signal<Vehicle | undefined>(undefined);
33
34 // Retrieve data into a signal
35 vehiclesResource = httpResource<VehicleResponse>(this.vehicleUrl);
36 vehicles = computed(() => this.vehiclesResource.value()?.results);
37
38 // Retrieve related data when a signal changes
39 filmResource = httpResource<Film>(() =>
40 this.selectedVehicle()?.films[0]);
41 film = this.filmResource.value;
42
43 // React to changes and recompute
44 total = computed(() =>
45 (this.selectedVehicle()?.cost_in_credits ?? 0) * this.quantity());
46 color = computed(() => this.total() > 50000 ? 'green' : 'blue');
47
48 qtyEffect = effect(() => console.log(this.quantity()));
49}
50
51export interface VehicleResponse {
52 count: number;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected