我接受的事实是,网络开发正在发生变化,我需要与RxJS紧密合作。我目前正在使用Angular 2.0.0-beta.15开发一个Web应用程序。RxJS主题和Angular2组件
我跟随最优秀的ng-book 2(这种广告是否允许?哦)。它涵盖了RxJS中的一些重要概念,并提供了进一步阅读的链接。我已经阅读并理解了源代码和相关的解释,但是我们在关于Subject
的某些细节,以及Angular 2组件如何使用这些主题流方面留下了一些细节。
我已增加从书正是如此代码:
export class SubmitResourceComponent {
private _newResourceTags: Subject<Tag> = new Subject<Tag>();
private _resourceTags: Observable<Tag[]>;
private _tagUpdates: Subject<any> = new Subject<any>();
private _create: Subject<Tag> = new Subject<Tag>();
private _remove: Subject<Tag> = new Subject<Tag>();
constructor() {
this._resourceTags = this._tagUpdates
.scan((tags: Tag[], operation: ITagsOperation) => operation(tags), initialTags);
this._create
.map((tag: Tag): ITagsOperation => (tags: Tag[]) => _.uniq(tags.concat(tag)))
.subscribe(this._tagUpdates);
this._remove
.map((tag: Tag): ITagsOperation => (tags: Tag[]) => _.without(tags, tag))
.subscribe(this._tagUpdates);
this._newResourceTags.subscribe(this._create);
}
get resourceTags(): Observable<Tag[]> {
return this._resourceTags;
}
protected addTagToResource(tag: Tag): void {
this._create.next(tag);
}
protected removeTagFromResource(tag: Tag): void {
this._remove.next(tag);
}
}
而且我消费_resourceTags
这样的:
<button class="btn" *ngFor="#tag of resourceTags | async" (click)="removeTagFromResource(tag)">{{ tag.name }}</button>
我无法把握,甚至从出色的支持gitter论坛,这就是为什么用户界面显示所有推入_resourceTags
Subject
的标签。我会想象这个流就像一个橡皮管:一旦一个元素被推入Subject
并发布到任何Observer
(在这种情况下,UI元素),它会不会蒸发并消失?它留在流/管/ Subject
?界面元素如何订阅Subject
?我是否以错误的方式思考它?我需要完整的心理重组/移植吗?
这么多问题!
由于该使用RxJS 5,你应该链接到RxJS 5 docs for ['scan'](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-scan)。 – Dorus
好的,完成了。感谢提示。 –
“With | async Angular订阅了Observable(Subject),当它收到一个值时,它显示它。” - 我假设每次“收藏”被改变时,所有的标签都会被推送到Angular组件,通过添加或移除标签,是吗?它必须做,否则你不能删除一个标签,这段代码运行正常。我想我只需要创建大量日志来准确查看副作用发生的时间以及收集的结果。 – serlingpa