2017-09-14 9 views
4

アイテムのリストの一部を表示しようとしました(ページ番号)。 マイテンプレート:私のコンポーネントで地図の後に観測が表示されます

<div class="notif" *ngFor="let notif of paginate() | async"> 
    {{notif.name}} 
</div> 

私がない場合:

public notifications: Observable<Notification[]>; 
public paginate(): Observable<Notification[]> { 
    return this.notifications; 
    } 

これが動作しているが、私がしなければ:

public notifications: Observable<Notification[]>; 
public paginate(): Observable<Notification[]> { 
    return this.notifications.map((notif: Notification[]) => { 
    return notif; 
    }); 

それはもう動作しませんが(I」何が起こっているのかを理解するために機能を単純化しました)。 .mapは観測可能な権利を戻しますか?だから、両方の方法で動作するはずですか?

+0

yap!それは両方の方法で動作する必要がありますいくつかの愚かな間違いをした可能性があり、観察可能な部分はよく見える –

+0

あなたが得ているエラー/動作は何ですか? –

+0

最初のケースでは通知名が表示され、2番目のケースでは何も起こりません。ページに何も表示されません – Lempkin

答えて

0

Observable of Notification []を返す関数でNotification []を返そうとしているが、これはうまくいかないため、これは間違っています。 通知[]が必要な場合は、Observableに登録する必要があります。

Observableを別のタイプにマップすることはできません。

public paginate(): Observable<Notification[]> { 
    return this.notifications; 
    }); 


notificationArr: Notification[]; 

// This needs to be inside a method not in the class declaration of variables and methods 
paginate().subscribe((notif: Notification[]) => { 
return (this.notificationArr = notif); 

    }); // should populate your array 
+0

ngOnDestroy()中に通常退会を忘れないでください。 – Clyde

+1

.map()はObservableを返しますか?彼らがここで言うように:http://reactivex.io/documentation/operators/map.html私の目的は、テンプレートの非同期。だから、私は通知リストの一部だけを取得しようとしますが、それでも観察可能なものを返します。 map()、またはfilter()で可能ではありませんか? – Lempkin

+0

'async'が使用されているので、購読する必要はありません。さらに、クラス内にステートメントを書くことはできません。 – Skeptor

関連する問題