asdsajt
asdsajt

Reputation: 3

Angular Observable filtering doesn't working

I have an Observable array and I want to filter it by some properties. However, the filtering doesn't working. I tried debugging the code but it's like it doesn't go into the pipe(map(... section.

ngOnInit() {
    this.bookService.getBooks().subscribe(res => {
      this.store.dispatch(listBooks({ books: res }));
    });

    this.filterForm = this.formBuilder.group({
      name: [""],
      author: [""],
      release_year: [""]
    });

    this.filterForm.valueChanges.subscribe(val => {
      this.books$.pipe(map(books => 
        books.filter(b => {
          return b.name.startsWith(val.name) &&
          b.author.startsWith(val.author) &&
          b.release_year.startsWith(val.release_year)
        })
      ))
    })

Upvotes: 0

Views: 473

Answers (2)

Mohammadreza Mohammadi
Mohammadreza Mohammadi

Reputation: 137

this line of your code never execute this.books$.pipe( because you dont subscribe it.any observable need to subscribe to execute.

it is better change your code like below code:

  this.filterForm.valueChanges
  .pipe(
   withLatestFrom(this.books$),
   map(([val,books])>
      books.filter(b => {
        return b.name.startsWith(val.name) &&
        b.author.startsWith(val.author) &&
        b.release_year.startsWith(val.release_year)
      })
   ))
   .subscribe(books => {
    this.books=books;        
   ))  

Upvotes: 1

batman567
batman567

Reputation: 889

This doesn't look like a complete example - e.g. what is this.book$ ?

Additionally your pipe() doesn't seem to end with a subscribe() - and so will not ever do anything

Upvotes: 0

Related Questions