Angular 基于Firebase可观测值的Firebase可观测过滤器
首先,我将向您展示我正在使用的代码Angular 基于Firebase可观测值的Firebase可观测过滤器,angular,firebase,firebase-realtime-database,observable,angularfire2,Angular,Firebase,Firebase Realtime Database,Observable,Angularfire2,首先,我将向您展示我正在使用的代码 //html code <div *ngFor="let lis of (classes | async | classFilter)"> //component.ts code tied to the html page constructor(public classService: ClassService) { //just calling a service to do this: //this._af.database.l
//html code
<div *ngFor="let lis of (classes | async | classFilter)">
//component.ts code tied to the html page
constructor(public classService: ClassService) {
//just calling a service to do this:
//this._af.database.list('/Class');
this.classes =this.classService.getObservable();
}
export class ClassFilterPipe implements PipeTransform {
constructor(
public scheduleService: ScheduleService,
public UserService: UserService) {
}
transform(value: any, args?: any): any {
if(!!value)
return value.filter(value2 => {
let bool = true;
//just getting the currently logged in user so i can grab
//the key of their schedule in firebase
this.UserService.getUser().subscribe(user => {
//grab the schedule based on that key from firebase
this.scheduleService.getObjectObservable(user.schedule).subscribe(schedule => {
//schedule is just the form: {key1:true,key2:true...}
//where the keys are associated to class keys in firebase
bool = schedule.hasOwnProperty(value2.$key);
});
});
return bool;
});
}
}
//html代码
//绑定到html页面的component.ts代码
构造函数(公共类服务:类服务){
//只需调用服务即可执行此操作:
//这个.u af.database.list('/Class');
this.classes=this.classService.getObservable();
}
导出类ClassFilterPipe实现PipeTransform{
建造师(
公共scheduleService:scheduleService,
公共用户服务:用户服务){
}
转换(值:any,args?:any):any{
如果(!!值)
返回值。筛选器(值2=>{
让布尔=真;
//只是获取当前登录的用户,以便我可以抓取
//他们日程安排的关键在firebase
this.UserService.getUser().subscribe(用户=>{
//从firebase获取基于该键的时间表
this.scheduleService.getObjectObservable(user.schedule).subscribe(schedule=>{
//时间表的形式是:{key1:true,key2:true…}
//其中键与firebase中的类键关联
bool=schedule.hasOwnProperty(值2.$key);
});
});
返回布尔;
});
}
}
基本上,我想做的是显示Firebase中特定用户的类。所以我现在要做的是获取所有类的列表,然后使用管道转换,我将使用来自用户的数据过滤页面上显示的数据。我遇到了麻烦,因为我需要使用这些信息来过滤我需要从Firebase获取的类。因此,异步获取该信息存在问题。我不知道一旦我有了其他数据,是否有办法过滤这些数据,或者我能做些什么。如果有人对我如何过滤我的数据有任何想法,请让我知道。谢谢大家!