I have an angular APP that need dynamically get the data from API. when I use static URL its work properly.
here my code
service.ts
export interface MachineData {
name: string;
status: string;
}
@Injectable()
export class MachineService {
BASE_URL = 'example.com/api/v1/id/1';
constructor(private http: HttpClient) { }
getMachineData() {
return this.http.get(this.BASE_URL);
}
}
component.ts
showMachine() {
this.machineService.getMachineData()
.subscribe((data: machineData) => this.machineData = {
status: data['status'],
});
}
markers: marker[] = [
{
id: 1,
name: "Marker A",
lat: -6.949065,
lng: 107.592339,
},
{
id: 2,
name: "Marker B",
lat: -6.935582,
lng: 107.610037,
}
]
component.html
<agm-marker
*ngFor="let m of markers; let i = index"
(markerClick)="showMachine(m.id, i)"
[latitude]="m.lat"
[longitude]="m.lng"
[label]="m.name">
<agm-info-window>
<strong>
<p>Status : {{machineData.status}}</p>
</strong>
</agm-info-window>
</agm-marker>
how to make the URL dynamic?... I mean add the ID to the URL.
example:
from this
return this.http.get(this.BASE_URL);
to this
return this.http.get(this.BASE_URL + id);
so I can retrieve data by id. already trying by do like this :
on my component.html
(markerClick)="showMachine(m.id)"
on my component.ts
showMachine(id: number) {
this.machineService.getMachineData(id: number)
.subscribe((data: machineData) => this.machineData = {
status: data['status'],
});
}
and my service.ts
return this.http.get(this.BASE_URL + id);
but it's not work.