How to apply rxjs operator to data got from angularfire2

我怕爱的太早我们不能终老 提交于 2019-12-23 04:21:17

问题


Suppose I requested for data in angularfire2 like this:

var ref = new Firebase("firebase url");

Now how can I apply rxjs operator like map operator to the data coming


回答1:


I don't think you are looking at angularfire2 there - that's v1 code...

You can use the rxjs operators in angularfire2 - the data is returned as an Observable (FirebaseListObservable or FirebaseObjectObservable) - so you can import and apply any of the rxjs operators to those...

In general, you just assign the data to a variable and have the template handle it with the async pipe

@Component({
  selector: 'app',
  templateUrl: `
  <ul>
    <li *ngFor="let item of items | async">
      {{ item.name }}
    </li>
  </ul>
  `,
})
class AppComponent {
  items: FirebaseListObservable<any>;
  constructor(af: AngularFire) {
    this.items = af.database.list('/items');
  }
}

But you can use the rxjs operators (be sure to import what you want)

this.af.database.list('/items')
.map(value => {
    console.log(value);
    return value; // do something with value etc.
})
.subscribe(data => console.log(data));

More here: AngularFire2



来源:https://stackoverflow.com/questions/37959306/how-to-apply-rxjs-operator-to-data-got-from-angularfire2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!