I am very new to Angular(started today) and I'm simple trying to extend a tutorial from datafiles to the Django REST Framework and API calls. I cannot work out how to pass a variable to an http call to a Django Rest Framework api.
I have my app module / route specified thus:
import { BarDetailsComponent } from './bar-details/bar-details.component';
const routes: Routes = [
...
{ path: 'bar-details/:id', component: BarDetailsComponent },
...
]
@NgModule({
declarations: [
...
BarDetailsComponent,
...
],
imports: [
BrowserModule,
RouterModule.forRoot(routes),
HttpModule
],
providers: [
...
BarDetailsProvider,
...
],
bootstrap: [AppComponent]
})
export class AppModule {
}
BarDetailsComponent
import { Component, OnInit } from '@angular/core';
import { Bar } from '../bars/bar';
import { BarDetailsProvider } from './bar-details.provider';
@Component({
selector: 'app-bar-details',
templateUrl: './bar-details.component.html',
styleUrls: ['./bar-details.component.scss']
})
export class BarDetailsComponent implements OnInit {
bar: Bar;
private selectedId : number;
constructor(private barDetailsProvider: BarDetailsProvider) { }
ngOnInit() {
this.barDetailsProvider.getBarDetails().subscribe(bar => this.bar = bar);
}
}
BarDetailsProvider
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Bar } from '../bars/bar';
import 'rxjs/add/operator/map';
import { environment } from '../../environments/environment';
@Injectable()
export class BarDetailsProvider {
constructor(private http: Http) { }
getBarDetails(): Observable<Bar> {
return this.http.get(`${environment.baseUrl}/bar/1`).map(response => response.json() as Bar);
}
}
Where I have return this.http.get(${environment.baseUrl}/bar/1).map(response => response.json() as Bar); above I want to replace 1 with the id which gets passed from the following html
<h1>Bars</h1>
<div *ngFor="let bar of bars">
<a routerLink="/bar-details/{{bar.pk}}">
<h2>Name: {{bar.name}}</h2>
</a>
<div>Address: {{bar.address}}</div>
<hr>
</div>
How do I get the id I'm passing back into the get call?