【发布时间】:2018-02-16 18:31:17
【问题描述】:
我正在努力订阅 Angular 4 中的选择下拉更改。
我的searchType 变量的更改仅在通过单击事件调用test() 函数后可见,但我需要Angular 立即订阅更改。
旁注:
- 我正在订阅过滤器管道。
- 服务在模块中提供,因此在同一个实例上工作
App.component.html
<select name="searchType" id="searchType" #searchType>
<option value="movie_title"> movie title</option>
<option value="director">director</option>
<option value="actor"> actor</option>
</select>
App.component.ts
@ViewChild('searchType') select: ElementRef;
searchType: number;
constructor(private service: ServiceService) {}
ngOnInit() {
this.searchType = this.select.nativeElement.options.selectedIndex;
this.service.sendSearchType(this.searchType);
}
test() {
this.searchType = this.select.nativeElement.options.selectedIndex;
this.service.sendSearchType(this.searchType);
}
service.service.ts
searchType = new Subject<number>();
sendSearchType(id: number) {
this.searchType.next(id);
}
getSearchType(): Observable<number> {
return this.searchType.asObservable();
}
最后 filter.pipe.ts 订阅更改
searchType: number;
private subscription: Subscription;
constructor(private service: ServiceService) {
this.service.getSearchType().subscribe(
(id) => (this.searchType = id)
);
}
transform(value: any[], filter: string): any[] {
filter = filter ? filter.toLocaleLowerCase() : null;
return filter ? value.filter(
(product) =>
this.auxiliaryFunction(product, filter)
) : value;
}
auxiliaryFunction(product, filter) {
if (this.searchType === 2) {
return (product.actor.toLocaleLowerCase().indexOf(filter) !== -1)
} else if (this.searchType === 1) {
return (product.director.toLocaleLowerCase().indexOf(filter) !== -1)
} else {
return (product.movie.toLocaleLowerCase().indexOf(filter) !== -1)
}
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
我哪里做错了? 将不胜感激任何解决方案。
【问题讨论】:
-
管道用于纯函数,在这种情况下你不应该使用管道,而是将过滤逻辑放在服务中,在你的服务中创建 searchType 主题并订阅它
<select name="searchType" (change)="service.searchType.next($event.target.value)"> -
你说得对,我实现了你关于将逻辑放入服务的评论,但我不太明白第二部分 - 代码(抱歉,我是初学者;)。我应该改变什么以遵循最佳实践惯例。我把我已经工作的代码放在 [link]stackblitz.com/edit/… 下,你能看看并推荐其他东西是否可以优化吗?
-
更新:不幸的是,如果我尝试将逻辑外包到服务中,则“搜索依据”过滤器 stos 可以正常工作。问题是我必须再次通过主题传递值 searchType 值 - 甚至可以使用相同的主题还是我必须为此创建另一个主题?
标签: angular observable subscription subject-observer