我尝试显示项目列表的一部分(分页).
我的模板:
我的模板:
<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返回一个可观察的权利?它应该双向工作吗?
解决方法
这是错误的,因为您尝试在返回Observable of Notification []的函数中返回Notification [],因此无效.
如果您需要Notification [],那么您需要订阅Observable.
如果您需要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