0
votes

I have my app hooked up to Firebase using the AngularFire2 package. I know it's hooked up because when I did the following in a component:

constructor(af: AngularFire) {
    this.items = af.database.list('/items');
    this.items.push({ attr: 'value' });
}

the item is pushed to the proper list in Firebase. So the connection is there. However, this.items, when trying to loop through and show them in the template, is null. What else needs to be done to show those items?

<div *ngFor="let item of items | async">{{ item.attr }}</div>

Edit

Here's a link to a gist with the actual code, although it's really almost exactly like what's here in this question.

https://gist.github.com/pjlamb12/0c3c680e3481c222f9b253224c7dd439

1

1 Answers

0
votes

You need to subscribe to the list, so you would need to do something like this:

so if you had in your database a list of items like so:

-items
   -item1
      -name: 'mycoolitem'
   -item2
      -name: 'mycoolitem2'

Then in your TS file:

//Items would have to be a list observable
items: FirebaseListObservable<any>;
//create an array for the returned items
itemsReturned: Array<any>;

constructor(af: AngularFire) {
    this.items = af.database.list('/items');
    this.items.subscribe((res) => {
      this.itemsReturned = res;
    });
}

Then in your HTML:

<div *ngFor="let item of itemsReturned | async">{{ item.name }}</div>

This would return:

mycoolitem
mycoolitem2

Hope this helps!