如何将 rxjs 运算符应用于从 angularfire2 获得的数据 [英] How to apply rxjs operator to data got from angularfire2
问题描述
假设我像这样在 angularfire2 中请求数据:
Suppose I requested for data in angularfire2 like this:
var ref = new Firebase("firebase url");
现在如何将 rxjs 运算符(如 map 运算符)应用于即将到来的数据
Now how can I apply rxjs operator like map operator to the data coming
推荐答案
我不认为你在看 angularfire2 - 那是 v1 代码...
I don't think you are looking at angularfire2 there - that's v1 code...
您可以在 angularfire2 中使用 rxjs 运算符 - 数据作为 Observable(FirebaseListObservable 或 FirebaseObjectObservable)返回 - 因此您可以导入任何 rxjs 运算符并将其应用于那些......
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');
}
}
但是你可以使用 rxjs 操作符(一定要导入你想要的)
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));
更多信息:AngularFire2一个>
这篇关于如何将 rxjs 运算符应用于从 angularfire2 获得的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!