2
votes

Below is a simple sample Angular 2 component which uses rxjs Observables.

import { Component , OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import  'rxjs/add/observable/of';

    @Component({
    selector:'ob',
    template: `

    <ul>
        <li *ngFor="let x of obj | async">
            {{x}}
        </li>
    </ul>    
    `
})

export class ObComponent implements  OnInit{

obj:Observable<number>;

ngOnInit() {
this.obj = Observable.of(1,2,3,4);
}

} 

This component throws below error

inline template:15:12 caused by: Cannot find a differ supporting object '4' of type 'number'. NgFor only supports binding to Iterables such as Arrays.

Any idea what could be the issue ?

1

1 Answers

2
votes

Observable.of. emits values in a sequence so *ngFor receives single values through async instead of the array that *ngFor expects.

Use

obj:Observable<number[]>;//declare

Observable.of([1,2,3,4])