I have a model:
export class Employe {
constructor(public id?: any,
public nom?: String,
public prenom?: String,
public cin?: String){}
}
employees.component.ts
ngOnInit(){
this.loadEmployes();
}
pageEmployes:any={};
loadEmployes():Observable<any>{
this.http.get("http://localhost:8080/api/employe").subscribe(
data=>{
console.log(data);
this.pageEmployes = data;
}, err=>{
console.log(err);
}
);
return this.pageEmployes;
}
employees.component.html
<tr *ngFor="let item of pageEmployes">
<td>{{item.nom}}</td>
<td>{{item.prenom}}</td>
</tr>
CollaborateurController.java
@RestController
@RequestMapping("/api/employe")
@CrossOrigin("*")
public class CollaborateurController {
@Autowired
private CollaborateurRepository collaborateurRepository;
@RequestMapping(value="", method=RequestMethod.GET)
public List<Collaborateur> getEmp() {
return (List<Collaborateur>) collaborateurRepository.findAll();
}
This throws me error:
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
Access to XMLHttpRequest at 'http://localhost:8080/api/employe' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
You are trying to iterate (loop) an object, not an array by the looks of you code example.
This part is false:
pageEmployes:any={};
And should be changed to:
pageEmployes: Employe[] = [];
The "= {};" initializes a new object.
The "= [];" initializes a new iterable list