1

In my service method that returns Observable I'm trying to notify the component via Subject that an action completed.

completed: Subject<boolean>

  constructor(private http: Http) {

  }

  loadItems(): Observable<FrontItemDto[]> {
    return this.http.get ( `${ServiceSettings.ApiUrl}/front` )
      .map ( res => {
        res.json ();
        if ( res.json () ) {
          this.completed.next ( true );
        }
      } )
      .catch ( (error: any) => Observable.throw ( error.json ().error || 'Server error' ) );
  }

This is how the component is listening to Subject:

ngOnInit(): void {
    this.getItems();
    this.sub = this.dataService.completed.subscribe(completed => {
      if (completed) {
        this.show = false;
      }
      });
  }

But I'm getting an error saying that Subject (completed) is undefined. What am I doing wrong?

risingTide
  • 1,754
  • 7
  • 31
  • 60
andrey.shedko
  • 3,128
  • 10
  • 61
  • 121

1 Answers1

8

You have not initialized completed Subject correctly

completed = new Subject<boolean>(); //it should have parenthesis at the end

Demo by @Ompurdy

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299