【发布时间】:2018-07-10 15:52:11
【问题描述】:
我正在尝试过滤 Observable 并在我的视图中循环使用它,但它不会呈现。
this.client_comments$ = this.comments$.filter(comment => comment.commentable_type == 'Client')
comments$ 在哪里:
comments = new BehaviorSubject([]);
this.comments.next(this.client.comments) // this.client.comments is an array of comment objects
this.comments$ = this.comments.asObservable()
结果是正确的,但是当我尝试使用异步管道渲染它时,什么都没有显示:
<p *ngFor="let comment of client_comments$ | async">{{ comment.body }}</p>
在查看 devtools 中的对象后,我发现似乎有一个嵌套的 observable(2 个来源):
client_comments$: Observable
operator: FilterOperator {predicate: ƒ, thisArg: undefined}
source: Observable
source: BehaviorSubject
...
value: (...)
而我的其他可观察对象具有以下结构(1 个来源):
comments$: Observable
operator: FilterOperator {predicate: ƒ, thisArg: undefined}
source: BehaviorSubject
...
value: (...)
有没有办法让我的 client_comments$ 在 n async 管道中工作? 请注意client_comments$ observable 数组包含所有需要的值
这是fiddle showing the structure change you can inspect in the console
【问题讨论】:
-
comments$是什么? -
comments$: Observable<any[]>。这是一个 Observable 列表 -
过滤
comments$之后好像改变了observable的结构 -
你能展示创建
comments$的代码吗? -
我已经在上面添加了
标签: angular asynchronous filter rxjs observable