无法显示从Firebase检索到的数据

时间:2016-03-31 16:43:28

标签: angularjs angular

我正在创建一个关于山脉的angular2应用程序。我正在尝试从Firebase检索并显示一些数据。 我拥有的文件是这些(我删除了boilderplate代码)

山list.component.ts

import {Pipe, PipeTransform} from 'angular2/core';
import {MapToIterable} from "./mypipe-pipe";

@Component({
    selector: 'my-mountain-list',
    template: `
    <h1>ALL THE MOUNTAINS</h1>
        <button (click)="onGetMountain()">Get Mountains</button>
        <br>
        <ul>
            <li *ngFor="#item of mountains | MapToIterable" (click)="onSelect(item)">
                This is the key {{item.key}} and this is the value {{item.value}}<br>
            </li>
        </ul>
    `,
    pipes: [MapToIterable]
})

export class MountainListComponent implements OnInit {
    ngOnInit(): any {
        this.mountains = this._mountainService.getAllMountains();
    }
}

mypipe-pipe.ts

import {Pipe} from "angular2/core";
import {Mountain} from "./mountain";

@Pipe({
  name: 'MapToIterable'
})
export class MapToIterable {
  transform(dict: Mountain[]): any {
    var a = [];
    for (var key in dict) {
      if (dict.hasOwnProperty(key)) {
        a.push({ key: key, val: dict[key] });
      }
    }
    return a;
  }
}

mountain.service.ts

import {Injectable} from "angular2/core";
import {Http, Headers} from "angular2/http";
import 'rxjs/Rx';
import {Observable} from "rxjs/Observable";
import {Mountain} from "../protected/mountain/mountain";

@Injectable()
export class MountainDataService {
    constructor(private _http: Http) {}

    getAllMountains(): Observable<any> 
    {
        const token = localStorage.getItem('token') !== null ? '?auth=' + localStorage.getItem('token') : '';
        return this._http.get('https://xxxxx-xxxx-xxxx.firebaseio.com/users/data.json' + token)
            .map(response => response.json());

    }
}

让我简要解释这3个文件: mountain-list.component.ts 必须显示我在Firebase中拥有的所有山脉的列表, mountain.service.ts ,它实现了从Firebase返回所有山脉的getAllMountains()方法, mypipe-pipe.ts 文件。如果我删除此文件,则会收到与this问题相关的错误。 使用mypipe-pipe.ts时,错误会显示错误,但是mountain-list.component.ts中的for循环不会打印山脉,这就是问题所在。而不是山脉列表我得到了这个:

This is the key _isScalar and this is the value 
This is the key source and this is the value 
This is the key operator and this is the value

仅供参考:我确信getAllMountains()从Firebase检索数据。

0 个答案:

没有答案