angular - map 后显示可观察

标签 angular rxjs

我尝试显示项目列表的一部分(分页)。 我的模板:

<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/

相关文章:

Angular 5 : Lazy-loading of component without routing

Angular - 管道函数中断执行流程

angular - RxJS:debounceTime 返回所有值

javascript - Angular2 从服务调用组件

javascript - 为什么这个冒泡排序不起作用

html - 嵌套 *ngFor* 与 *ngIf

Angular2 模板表达式在变更检测时为每个组件调用两次

javascript - Typescript 使用 Rx.js 过滤器运算符区分联合类型?

javascript - RxJs:经过一段时间后才有新值

javascript - Spring 和 Angular - 非常简单的 REST 请求