How to return value inside subscribe Angular 4

前端 未结 4 1969
梦毁少年i
梦毁少年i 2020-12-30 22:07

I\'m new to observables in angular. I have a problem wanting to return a value inside a subscribe method. I have the following method (getFirebaseData(idForm:string):o

4条回答
  •  再見小時候
    2020-12-30 22:21

    You can't directly return totalQuestions like that, you need to use a subject to achieve that.

    getTotalQuestions(idForm:string): Observable {
    let totalQuestions:number;
    var subject = new Subject();
    this.getFirebaseData(idForm+"/Metadatos")
    .subscribe(items => {
        items.map(item => {
    
          totalQuestions=item.Total;
          console.log(totalQuestions);
          subject.next(totalQuestions);
        });
      }
    );
      return subject.asObservable();
    }
    

    Usage: getTotalQuestion(idForm).subscribe((r)=>console.log(r))

提交回复
热议问题