我有列表,应该模拟流:
list = [
{name : 'Str1', age: 10},
{name : 'Str2', age: 10},
{name : 'Str3', age: 10}
];
我已从此列表中创建了Observable
:
Observable.from(this.list).skip(this.currentPage * this.pageSize).take(this.pageSize).subscribe(data => this.pagerList = data, console.error);
在订阅方法中,我逐个获取值。我应该如何等待返回整个数据然后获取整个列表。有take()运算符,之后Observable必须停止。
我不想在数组中逐个放置每个值。
我是新来的,不仅是角度,还有javascript。
答案 0 :(得分:7)
您是否尝试使用toArray
运营商?。
let list = [
{ name: 'Str1', age: 10 },
{ name: 'Str2', age: 10 },
{ name: 'Str3', age: 10 },
{ name: 'Str4', age: 10 },
{ name: 'Str5', age: 10 },
{ name: 'Str6', age: 10 }
];
let currentPage = 2;
let pageSize = 2;
Rx.Observable.from(list)
.skip(currentPage * pageSize)
.take(pageSize)
.toArray()
.subscribe(data => console.log(data), console.error);

<script src="https://npmcdn.com/@reactivex/rxjs@5.0.3/dist/global/Rx.min.js"></script>
&#13;
答案 1 :(得分:3)
scan
运算符应该执行您想要的操作
Observable.from(this.list)
.skip(this.currentPage * this.pageSize)
.take(this.pageSize)
.scan([], acc, curr) => {acc.push(curr); return acc;});
.subscribe(data => this.pagerList = data, console.error);
http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-scan
答案 2 :(得分:1)
我认为这是更合适的解决方案:
Observable.of(this.list).map(x => x.slice(this.currentPage * this.pageSize)).map(x => x.slice(0, this.pageSize)).subscribe(data => this.pagerList = data, console.error);