Search code examples
angularobservablesubscriptionsubject-observer

Subscription to Subject in a pipe doesn't work; Angular4


im struggling with subscribing to select dropdown change in Angular 4. The change of my searchType variable is only visible after calling a test() function through click event but ofc I need Angular to subscribe to changes instantly.

Sidenote:

  • I'm subscribing in the filter pipe.
  • Service is provided in the module so working on the same instance

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();
 }

and finally filter.pipe.ts subscribing to the change

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();
    }

Where did i go wrong ? Will be grateful for any solutions.


Solution

  • You have to watch for changes on select element. I made this live example, I hope it helps. (app.component.ts)

    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/fromEvent';
    import { AfterViewInit } from '@angular/core';
    
    ...
    
    ngAfterViewInit() {
        const $selector = Observable.fromEvent(this.select.nativeElement, 'change');
        $selector.subscribe(() => {
            this.searchType = this.select.nativeElement.options.selectedIndex;
            this.service.sendSearchType(this.searchType);
        });
    }
    

    (filter.pipe.ts)

    constructor(private service: AppService) {
        this.service.getSearchType().subscribe(
            (id) => { 
                console.log(`${id}px`);
                return (this.searchType = id);
            }
        );
    }