在我的顶栏组件中,我有一个带搜索字段的小表单。
在我的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
被释放时会处理,但我想确定。