【发布时间】:2019-02-06 19:56:03
【问题描述】:
我目前正在尝试掌握 RxJs 的功能。首先,我想将两个 observable 与“concat”函数结合起来。没有成功,很遗憾。这是我的代码:
组件 =>
import { Component, Input, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { interval, range, concat } from 'rxjs';
@Component({
selector: 'app-appareils',
templateUrl: './appareils.component.html',
styleUrls: ['./appareils.component.scss']
})
export class AppareilsComponent implements OnInit {
constructor(private appareilsService : AppareilsService ) {
}
ngOnInit() {
var counter = interval(1000)
var sequence = range(1, 10)
var concat(counter,sequence)
concat.subscribe(
(value) => {
this.rxjsValue = value
},
(error) => {
console.log('Uh-oh, an error occurred! : ' + error);
},
() => {
console.log('Observable complete!');
}
)
}
}
模板 =>
<p>
{{ rxjsValue }}
</p>
所以想法是每一秒,rxJsValue 在模板中被编辑并呈现给查看。由于我严格遵守文档,我很困惑,我不得不承认:http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-concat
我看不出逻辑中的缺陷,谁能指出问题出在哪里?
【问题讨论】:
-
而不是
var concat(counter,sequence),也许是const concat = concat(counter, sequence),它返回一个你可以订阅的可观察对象 -
您是否打算显示一个以
1开始并以10结束的计数器,每秒更新一次? stackblitz.com/edit/angular-lm2aaz -
谢谢你们的回答。 Linhh,我已经尝试了您的解决方案,但它以未定义的错误迎接我。有点奇怪,看起来该功能根本无法识别。亚历山大,是的,这正是我的观点!
标签: angular rxjs observable