call property out subscribe angular 8

public GetTranslation = (SourceEnglish: string, LanguageId: number, CompanyId: number): string => {
  this.api.GetTranslate(SourceEnglish, LanguageId, CompanyId).subscribe(
    (res: any) => {
      this.SourceResult = res[0].SourceResult;
      // Work Done
      console.log(this.SourceResult);
    },
    err => {
      console.log(err);
    }
  );
  // undefined
  console.log(this.SourceResult);
  return this.SourceResult;
}

1 answer

  • answered 2019-11-14 05:22 Adrian Brand

    Use the async pipe

    translatedText$ = this.api.GetTranslate(SourceEnglish, LanguageId, CompanyId).pipe(
      map(res => res[0].SourceResult)
    );
    

    and in the template

    {{ translatedText$ | async }}
    

    No need for any subscriptions as the async pipe manages the subscribing for you.