How to return value inside subscribe Angular 4

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

    To solve this I would create a property totalQuestions$ an observable

    In the TS file

    totalQuestions$ = this.getFirebaseData(idForm + "/Metadatos").pipe(
      map(items => items.reduce((prev, {Total}) => prev + Total), 0)))
    )
    

    And Now in your template you can use async pipe

    {{ totalQuestions$ | async }}
    

    If you would like to use the variable in your TS file, then you can use combination operators like forkJoin or combineLatest to access this value

    newVariable$ = combineLatest([totalQuestions$, ...]).pipe(
      map(([totalQuestions, ... ]) => {
        // Perform an operation here and return a value
      })
    )
    

提交回复
热议问题