将AngularFire firebaseListObservable对象转换为普通对象

时间:2017-02-25 22:29:16

标签: javascript datatables firebase-realtime-database angularfire2

我正在使用datatables(datatables.net),我想使用AngularFire2从Firebase获取数据的数据来获取对象。

但是,这会得到一个FirebaseListObservable对象,我不知道如何在数据表中使用它,我之前使用过普通对象。

有没有办法将FirebaseListObservable转换为普通对象以便以这种方式使用?

component.html

<sa-datatable
            [options]="{
              data: rfis,
              columns: [
                {data: 'rfiNo'},
                {data: 'title'},
                {data: 'status'}
                ]
             }"
            filter="true" tableClass="table table-condenced table-striped table-bordered"
            paginationLength="true" tableClass="table table-striped table-bordered table-hover"
            width="100%">
            <thead>
              <tr>
                <th data-class="expand"><i
                  class="fa fa-fw fa-user text-muted hidden-md hidden-sm hidden-xs"></i>
                  Lot #
                </th>
                <th>
                  Lot Name 1
                </th>
                <th>
                  Lot Name 2
                </th>
              </tr>
            </thead>
          </sa-datatable>

component.ts

import { Component, OnInit } from '@angular/core';
import {FadeInTop} from "../shared/animations/fade-in-top.decorator";
import {RfisData} from "../providers/rfis-data";

@FadeInTop()
@Component({
  selector: 'app-lots',
  templateUrl: './lots.component.html',
  providers: [RfisData]
})
export class LotsComponent implements OnInit {
  rfis: any;
  data: any;

  constructor(
    public rfisService: RfisData
  ) {}

  ngOnInit() {
    this.rfis = this.rfisService.getRfis();
    this.data = this.rfisService.getData();
  }
}

provider.ts

import { Injectable } from "@angular/core";
import { AngularFire, FirebaseListObservable } from 'angularfire2';

@Injectable()
export class RfisData {
  rfis: FirebaseListObservable<any>;
  data: any; 

  constructor(
    af: AngularFire
  ) {

// this just to compare object types
    this.data =[
        {
          "rfiNo": "Frist RFI",
          "status": "open",
          "title": "Its a fucking RFI dude"
        }
      ];    

    this.rfis = af.database.list('/rfis');

  }

  getRfis(){
    console.log(this.rfis);
    return this.rfis;
  }

// this just to compare object types
  getData(){
    console.log(this.data);
    return this.data;
  }

}

控制台日志的结果显示了对象的两个结果,一个是firebaseListObservable对象,另一个是普通的[Object]。

谢谢。

1 个答案:

答案 0 :(得分:1)

这对我有用:

af.database.object('/some/ref/to/object')
    .map(val => Object.assign(new MyPlainObject(), val);