2016-03-09 130 views
1

所以我得到這個當我嘗試從火力無效參數管道 'AsyncPipe'

Invalid argument '{"-KCO4lKzEJPRq0QgkfHO":{"description":"teste","id":1457488598401,"resourceUrl":"tete","title":"test2"}}' for pipe 'AsyncPipe' in [listItems | async in [email protected]: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()); 
    } 
} 

我知道我試圖以錯誤的方式顯示我的數據,但我不知道如何解決這個問題。任何幫助讚賞。

回答

1

async管道工作與observables和/或承諾。它確實認購你,所以你只需要通過一個可觀察而不訂閱它在你的代碼:

ngOnInit(): any { 
    this.listItems = this._firebaseService.getResources() 
    } 
+0

您好感謝您的意見,現在我得到這個'無法鰭d [listItems |中有不同的支持對象'[object Object]'異步在ArcListComponent @ 2:10]'請注意,我不得不將我的listItems類型更改爲Observable 也。 –

+0

@PetrosKyriakou你的Observable/Promise必須返回一個數組,NgFor不會迭代對象。 –

+0

@EricMartinez有你的例子嗎?我對這類東西很陌生。我的意思是我明白你說的只是不知道該怎麼做 –

7

async管道預計可觀察或承諾。 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 ] 
}) 

看到這個問題的更多細節:

+0

這是否也意味着我不需要調用取消訂閱,因爲它是由框架處理的? – garethb

+0

異步管道自動爲您處理訂閱和取消訂閱。 – Muirik