Issue : How to populate JSON values retrieved from a REST API into a Select statement?
team.component.html
<mat-select placeholder="Select Name" [(ngModel)]="name">
<mat-option *ngFor="let currentName of nameValuesArray" [value]='currentName.name'>
{{currentName.name}}
</mat-option>
</mat-select>
team.component.ts
export class UpdateTeamRosterComponent implements OnInit {
nameValuesArray;
name;
constructor(private fetchData : FetchdataService) { }
ngOnInit() {
console.log('ngOnIt called');
this.nameValuesArray = this.fetchData.getAllEmployeeNames('DCP').subscribe(data=>{
this.nameValuesArray = JSON.stringify(data);
});
}
}
ERROR
UpdateTeamRosterComponent.html:1 ERROR Error: Cannot find a differ supporting object '[{"name":"Ajith"},{"name":"Anand"},{"name":"Bharath"}]' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3161)
at checkAndUpdateDirectiveInline (core.js:18623)
at checkAndUpdateNodeInline (core.js:19884)
at checkAndUpdateNode (core.js:19846)
at debugCheckAndUpdateNode (core.js:20480)
at debugCheckDirectivesFn (core.js:20440)
at Object.eval [as updateDirectives] (UpdateTeamRosterComponent.html:2)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:20432)
at checkAndUpdateView (core.js:19828)
at callViewAction (core.js:20069)
Data is fetching correctly from the server The issue lies in rendering the data onto the select statement