I am trying to create a simple array of strings(Iteratable) in typescript and use Angular ngFor to display all of them on my page. I tried several ways of creating an array but none of them return iteratable back.
the last effort I did looks like this.
vehicles: String[] = [];
ngOnInit() {
this.vehicles.push('vehicle 1');
this.vehicles.push('vehicle 2');
this.vehicles.push('vehicle 3');
this.vehicles.push('vehicle 4');
this.vehicles.push('vehicle 5');
console.log(this.vehicles);
}
I also tried
vehicles = ['Vehicle 1', 'Vehicle 2', 'Vehicle 3', 'Vehicle 4', 'Vehicle 5'];
or
vehicles: ['Vehicle 1', 'Vehicle 2', 'Vehicle 3', 'Vehicle 4', 'Vehicle 5'];
even
vehicles = <String[]>['Vehicle 1', 'Vehicle 2', 'Vehicle 3', 'Vehicle 4', 'Vehicle 5'];
but in all of the cases when I use ngFor:
<div *ngFor="let vehicle of vehicles">
<div class="row" #row>
<mat-card>{{vehicle}}</mat-card>
</div>
</div>
I get the error
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
when I print console.log(this.vehicles) I get the response as expected:
["Vehicle 1", "Vehicle 2", "Vehicle 3", "Vehicle 4", "Vehicle 5"]
how do I return the array as array Iterable and not object '[object Object]'?
var vehicles = [];and leave the rest as it is.. - Harshith Raivehicleas public.. - Torab Shaikh