N
N
Nubbin2018-05-28 12:37:28
Angular
Nubbin, 2018-05-28 12:37:28

Angular 2 ngFor?

Hello, I can't figure out why it's throwing an error.


ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

//Выдает ошибка именно в этом варианте
{"in":{"231":{'name': 'test1'}, "2351":{'name': 'test2'}, "1231":{'name': 'test3'}}


//Тут все нормально
{"in":{"0":{'name': 'test1'}, "1":{'name': 'test2'}, "2":{'name': 'test3'}}

this.getJson().subscribe((data:any) => {
this.listers        = data['in'];
}

<div *ngFor="let item of listers; let i=index"</div>

Guys please help, I just need the first option

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Demian Smith, 2018-05-28
@Nubbin

ngFor doesn't work with objects that can't be iterated. You need to cast data['in'] to an array. Try like this:
this.listers = Object.values(data['in']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question