11432

Observable and ngFor in Angular 2

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 ?

Answer1:

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])

Recommend

  • Google apps script and script db replacement example needed
  • run time annotation scanning when Dynamic class loading
  • Stringify object name in Lua
  • Capture literal brackets inside brackets in pyparsing
  • Angular5 Service Worker update(SWUpdate) not detecting on firefox. Working on chrome
  • Deploying pre-encrypted configuration files to a production environment
  • Instanciate service on startup in Angular2
  • Jquery Show & ScrollTop (or ScrollTo)
  • How to make the tableview response pan gesture in ZUUIRevealController
  • How to replace TouchesBegan with UIGestureRecognizer
  • get passwords from chrome
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Function JavaScript : on Menu CSS HTML
  • Can't delete li from to-do list
  • Cut the background to expose the layer below
  • Disable Kendo Autocomplete
  • Jquery popup on mouse over of calendar control
  • Django invalid literal for int() with base 10
  • xtable package: Skipping some rows in the output
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • C++ Partial template specialization - design simplification
  • Using jQuery closest() method with class selector
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • sending/ receiving email in Java
  • Web-crawler for facebook in python
  • Jquery - Jquery Wysiwyg return html as a string
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Java static initializers and reflection
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal