javascript - 管道 'AsyncPipe' 的参数无效

标签 javascript typescript firebase angular

所以当我尝试从 firebase 获取数据时我得到了这个

Invalid argument '{"-KCO4lKzEJPRq0QgkfHO":{"description":"teste","id":1457488598401,"resourceUrl":"tete","title":"test2"}}' for pipe 'AsyncPipe' in [listItems | async  in ArcListComponent@2:10]

ArcListComponent

import { Component, OnInit } from "angular2/core";
import { FirebaseService } from "../shared/firebase.service";
import { ArcItem } from "./arc-item.model";


@Component({
    selector: "arc-list",
    template: `
    <ul class="arc-list">
      <li *ngFor="#item of listItems | async " class="arc-item">
        <h3>{{ item.name}}</h3><a [href]="item.resourceUrl" target="_blank" class="btn btn-success pull-right"><span>Go</span></a>
        <hr>
        <blockquote>{{ item.description }}</blockquote>
        <hr>

      </li>
    </ul>
    `

})

export class ArcListComponent implements OnInit {
  listItems: string;

  constructor(private _firebaseService: FirebaseService) {}

  ngOnInit(): any {
    this._firebaseService.getResources().subscribe(
      resource => this.listItems = JSON.stringify(resource),
      error => console.log(error)
    );
  }

}

firebase_service

import { Injectable } from "angular2/core";
import { Http } from "angular2/http";
import "rxjs/Rx";

@Injectable()
export class FirebaseService {

  constructor(private _http: Http) {}

  setResource(id: number, title: string, description: string, resourceUrl: string) {
    const body = JSON.stringify({ id: id, title: title, description: description, resourceUrl: resourceUrl});

    return this._http
                     .post("https://######.firebaseio.com/resource.json", body)
                     .map(response => response.json());
  }

  getResources() {
    return this._http
                     .get("https://######.firebaseio.com/resource.json")
                     .map(response => response.json());
  }
}

我知道我试图以错误的方式显示我的数据,但我不知道如何解决这个问题。任何帮助表示赞赏。

最佳答案

async 管道需要一个 observable 或 promise。 http.getmap 运算符返回 observable,因此您可以将返回的对象设置到组件的 listItems 属性中。在这种情况下您不需要订阅:

this.listItems = this._firebaseService.getResources();

此外,这个元素将“接收”对象必须是一个数组才能在 ngFor 中使用它。您的服务返回一个对象而不是来自 Firebase 的数组。如果要遍历对象的键,则需要实现自定义管道:

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
    let keys = [];
    for (let key in value) {
      keys.push({key: key, value: value[key]);
    }
    return keys;
  }
}

并像这样使用它:

@Component({
  selector: "arc-list",
  template: `
  <ul class="arc-list">
    <li *ngFor="#item of listItems | async | keys" class="arc-item">
      <h3>{{ item.value.name}}</h3><a [href]="item.value.resourceUrl" target="_blank" class="btn btn-success pull-right"><span>Go</span></a>
      <hr>
      <blockquote>{{ item.value.description }}</blockquote>
      <hr>

    </li>
  </ul>
  `,
  pipes: [ KeysPipe ]
})

有关详细信息,请参阅此问题:

关于javascript - 管道 'AsyncPipe' 的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35881721/

相关文章:

javascript - 使用 javascript 永久更改 html 值?

angular - typescript 错误TS2322 : Type 'Observable<boolean | {}> - Angular 5 & Typescript 2.4.2

swift - 更新 Firebase 数据库

jsf/icefaces 中的 javascript

javascript - After Effects 脚本 : How to add Composition with start time display to an Composition

javascript - 在 JavaScript 中应用时区后获取日期的组成部分

html - 如何使用 Angular 6 向表格添加附加功能

typescript - 带有 TypeScript : Importing components into components 的 Angular2

android - Firebase Cloud Messaging 将服务器 key 替换为用于发送通知/消息的 token

android - 未找到 FirebaseAuthWebException。请验证 AAR