Angular 2清理Observables?

时间:2016-07-11 00:24:28

标签: angular observable

在我的顶栏组件中,我有一个带搜索字段的小表单。

在我的ngOnChanges中,我调用以下方法:

private setupForm(): void
{
    this.filterForm = this.formBuilder.group
    ({
        "title": [this.info.filters.searchFileName],
        "description": [this.info.filters.searchDescription],
        "director": [this.info.filters.searchDirector],
        "writer": [this.info.filters.searchWriter],
        "actors": [this.info.filters.searchActors],
    });

    this.filterForm.controls["title"].valueChanges.subscribe(val =>
    {
        this.info.filters.searchFileName = val;
        this.filterChanged.emit(this.info.filters);
    });
    this.filterForm.controls["description"].valueChanges.subscribe(val =>
    {
        this.info.filters.searchDescription = val;
        this.filterChanged.emit(this.info.filters);
    });

    // Omitted: same for the other 3 fields...
}

顶部栏是静态的,只有在我的任何搜索过滤器发生变化时才会触发ngOnChanges,这只会在用户互动时发生。

但是,每次创建5个订阅时,我都不确定在创建新unsubscribe之前是否需要专门this.filterForm。我假设垃圾收集在前一个this.filterForm被释放时会处理,但我想确定。

0 个答案:

没有答案