【问题标题】:Angular 2 and Firebase. How to do unique filter?Angular 2 和 Firebase。如何做独特的过滤器?
【发布时间】:2016-12-30 16:35:33
【问题描述】:

我正在尝试获取一些有关如何在 FirebaseListObservable 上进行独特过滤的信息。

我的数据:

-key 
-- name: john smith
-- dept: hr
-key
--name: sam brown
-- dept: sales
-key
--name: nick reyes
-- dept: hr

如何让 angular2 显示 hr 和 sales?

我可以用 angular 1 和 firebase 做到这一点。但我不确定如何处理 angular 2。我似乎无法从 https://github.com/angular/angularfire2 获得任何信息

【问题讨论】:

    标签: angular firebase firebase-realtime-database


    【解决方案1】:

    这可能只是一种解决方法,因为我在他们的 github 页面上找不到任何东西来获取唯一值。 我继续订阅它并在另一个数组中保存它,如果它还不存在的话:

    employees : FirebaseListObservable<Employee[]>;  this.employees = af.database.list('/employees'); this.departments=[];
    
    
        this.employees.subscribe(
        x =>{     
          x.forEach(element => {
          if(!this.departments.includes(element.department))
           { this.departments.push(element.department);}
    
        });        
      }   
      );
    

    现在我可以在我的选择中使用它:

    <select name="depts" id="" #d (change)="filterBy(d.value)">    <option *ngFor="let d of departments" [value]="d">{{d}}</option></select>
    

    【讨论】:

    • 还有其他方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2018-05-08
    • 2023-04-03
    相关资源
    最近更新 更多