Angular2:对Observable的调用太多

时间:2016-12-23 12:52:29

标签: angular service observable subscriber

所以目前我创建了一个服务,以便将数据从一个组件路径到另一个组件。我们说我有一个选择组件和一个组件工厂。为了将所选值从选择组件传递到工厂,我设计了此服务。

以下是该服务的外观:

var ordered_things = just_one_thing.orderBy('column2')

所以,正如我所说,我必须将属于select组件的html import { Injectable } from '@angular/core'; import {Subject} from "rxjs/Subject"; @Injectable() export class ObservableService { private dataSource = new Subject(); data = []; data$ = this.dataSource.asObservable(); updateFilter(value) { this.data.push(value); console.log('Changes transfer'); this.dataSource.next(this.data) } } 输入中的数据路径传递到工厂,这里是select组件html

select

以下是我从select组件向可观察服务发送选定值的方式:

<select (change)="onChange($event)">
    <option value="">Options</option>
    <option *ngFor="let option of options" [value]="option.value">{{ option.name }}</option>
</select>

}

以下是我在工厂组件中捕获数据的方法

private onChange(option) {
    console.log('Changes sent');
    this._observable.updateFilter(option);

您可能会注意到我在每个数据传输阶段都提供的控制台命令。事实上我得到了这个:

ngOnInit() {
    this._observable.data$.subscribe(
        data => console.log('Changes detected');
    )
}

事实上,可观察用户检测到了13个变化!请帮助我理解这种奇怪行为的来源?

0 个答案:

没有答案