我尝试显示项目列表的一部分(分页)。 我的模板:
<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;
});
它不再起作用(我简化了函数以了解发生了什么)。 .map 重新调整一个可观察的权利?所以它应该双向工作?
最佳答案
这是错误的,因为您试图在返回 Notification[] 的 Observable 的函数中返回 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
关于angular - map 后显示可观察,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221731/