I was on a project with Angular and a backend.
If I enter a list in the ts file:
this.towns = [{
code: '1',
label: 'Paris'
},
{
code: '0',
label: 'Vancouver'
}
];
I can display them in my html but not if I try to connect to a db:
<select class="selectpicker dropdown mt-4 ml-3" id="town">
<option *ngFor="let town of towns" [value]="town.id">{{town.name}}</option>
</select>
In my ts file, I tried:
export class TestComponent implements OnInit {
form: FormGroup;
towns: { id: number, name: string }[];
constructor(
private http: HttpClient
) { }
towns: Town[];
apiUrl = 'https://localhost:8000/api/';
ngOnInit(): void {
this.form = new FormGroup({
town: new FormControl()
});
this.getTown();
}
onClickTown(): Observable<any> {
console.log(this.form)
return this.http.get(this.apiUrl + 'towns')
}
getTown() {
this.http.get<any[]>(this.apiUrl + 'towns')
.subscribe(data => {
this.towns = data;
console.log(data)
});
}
postData() {
console.log("data");
this.http.post<any[]>(this.apiUrl, {}).
subscribe(
(data: any[]) => {
// console.log(data);
}
)
}
Did I forget a step? Thanks
towns: { id: number, name: string }[];andtowns: Town[];