Instead of calling the service in the .ts file and wasting time, I want to assign this as a global value that I can use. However, I keep getting undefined.
Here is my service file
@Injectable({
providedIn: 'root'
})
export class DeService {
data:any;
constructor(private http: HttpClient){}
getData(id:any):Observable<any>{
this.http.get(Url.getDetails+"id="+id).pipe(first()).subscribe(res=>{
this.data = res;
console.log(this.data) //Data successfully retrieved here
}
return this.http.get(Url.getDetails+"id="+id)
}
}
The ts file
export class text implements OnInit{
constructor(public de:DeService){}
ngOnInIt(){
console.log(this.de.data); //Returns undefined here
}
}