How to return value inside subscribe Angular 4

前端 未结 4 1965
梦毁少年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

    Observable runs when you subscribe to it and what return from subscription is always a subscription object like setInterval. first sample: because this is a async call, second console.log won't wait for subscribe to finish before it executes.

    getTotalQuestions(idForm: string) {
      let totalQuestions: number;
      return this.getFirebaseData(idForm + "/Metadatos").pipe(
         map(items =>
          items.map(item => {
            totalQuestions = item.Total;
            console.log(totalQuestions);
          });
        ));
    }
    
    getTotalQuestions('231').subscribe(console.log)
    

提交回复
热议问题