@@ -204,15 +204,16 @@ itemsRef.remove();
204204import { Component } from ' @angular/core' ;
205205import { AngularFireDatabase , AngularFireList } from ' angularfire2/database' ;
206206import { Observable } from ' rxjs/Observable' ;
207+ import ' rxjs/add/operator/map' ;
207208
208209@Component ({
209210 selector: ' app-root' ,
210211 template: `
211212 <ul>
212213 <li *ngFor="let item of items | async">
213214 <input type="text" #updatetext [value]="item.text" />
214- <button (click)="updateItem(item.$ key, updatetext.value)">Update</button>
215- <button (click)="deleteItem(item.$ key)">Delete</button>
215+ <button (click)="updateItem(item.key, updatetext.value)">Update</button>
216+ <button (click)="deleteItem(item.key)">Delete</button>
216217 </li>
217218 </ul>
218219 <input type="text" #newitem />
@@ -225,7 +226,10 @@ export class AppComponent {
225226 items: Observable <any []>;
226227 constructor (db : AngularFireDatabase ) {
227228 this .itemsRef = db .list (' messages' );
228- this .items = this .itemsRef .valueChanges ();
229+ // Use snapshotChanges().map() to store the key
230+ this .items = this .itemsRef .snapshotChanges ().map (changes => {
231+ return changes .map (c => ({ key: c .payload .key , ... c .payload .val () }));
232+ });
229233 }
230234 addItem(newName : string ) {
231235 this .itemsRef .push ({ text: newName });
0 commit comments