0
heroes$: Observable<Hero[]>;

 // Push a search term into the observable stream.
  search(term: string): void {
    this.searchTerms.next(term);
  }
 ngOnInit(): void {
    this.heroes$ = this.searchTerms.pipe(
      // wait 300ms after each keystroke before considering the term
      debounceTime(500),

      // ignore new term if same as previous term
      distinctUntilChanged(),

      // switch to new search observable each time the term changes
      switchMap((term: string) => this.searchService.searchHeroes(term)),
    );
  }

  onClear(event){
     console.log('clear observable');
       //clear array
      // I have Tried these two things to clear observable array but it not..
      this.heroes$.map(Hero => Hero = []);
      Observable.of<Hero[]>([]);
  }

I have a Searh box with search suggestion has observable Array.. during onClear event the the Observable Array should Empty.

8
  • you can do this, this.heroes$.splice(0) or simply you can do this.heroes$ = [] Commented Mar 21, 2018 at 5:35
  • Property 'splice' does not exist on type 'Observable<Hero[]>'. Commented Mar 21, 2018 at 5:39
  • this.heroes$ = [] have following error Type 'undefined[]' is not assignable to type 'Observable<Hero[]>'. Commented Mar 21, 2018 at 5:40
  • stackoverflow.com/questions/40971285/… Commented Mar 21, 2018 at 5:55
  • Observable.of<Hero[]>([]); i Have Tried this but not working.. please help me Commented Mar 21, 2018 at 6:01

1 Answer 1

1

You could try something like

this.heroes$ = this.searchTerms.pipe(
  // wait 300ms after each keystroke before considering the term
  debounceTime(500),

  // ignore new term if same as previous term
  distinctUntilChanged(),

  // switch to new search observable each time the term changes
  switchMap((term: string) => {
    if (term === '') return Observable.of<Hero[]>([]);
    else return this.searchService.searchHeroes(term);
  }),
);

(so this.heroes$ would emit [] if this.searchTerms emits '')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.