我想清除一个可观察的对象数组。我做了什么:
private pages: Observable<CustomPage[]>;
//clear
this.pages.map(p => p = []);
是否有直接的Observable方法(rxjs)可以在没有映射的情况下执行此操作?
答案 0 :(得分:3)
我发现这非常有效,而且非常简单。
this.someObservable = of([]); <-- Assigning empty array to an observable.
'的'是rxjs库的一部分,并返回一个observable,因此您可以在此处将其与赋值运算符一起使用。
希望这会有所帮助:)
答案 1 :(得分:0)
如果您来自英雄之旅教程,您的理解可能会受到他们在那里展示的非常基本的服务的限制。这是一个更完整的服务快速草图,可以做你想要的......
export class PageService {
private _pages = new ReplaySubject<CustomPage[]>(1);
private _dateFilter = new ReplaySubject<DateFilterCriteria>(1);
//Subscribe to this in your component
get pages(): Observable<CustomPage[]> {
return this._pages;
}
//Use this if you want to display the currently selected date
//filter somewhere (e.g. on your date filter control)
get dateFilter(): Observable<DateFilterCriteria> {
return this._dateFilter;
}
//PageAccess encapsulates requests against the Http service
constructor(private pageAccess: PageAccess) {
this._dateFilter.subscribe(dateFilter => this.fetchPages(dateFilter));
this.setNewDateFilter(null);
}
//Clears out the current list of pages, issues a new request
//for pages
private fetchPages(dateFilter: DateFilter) {
this._pages.next([]);
this.pageAccess.fetchPages(dateFilter).subscribe(pages => {
this._pages.next(pages);
});
}
//When your date filter control changes it should call this
//method
setNewDateFilter(dateFilter: DateFilter) {
this._dateFilter.next(dateFilter);
}
}
另外,我认为我可以互换使用DateFilter
和DateFilterCriteria
来表示您用来指示所需页面日期的某些类。
答案 2 :(得分:0)
又甜又甜
new Observable<[]>();