I don't want to call the service in the .ts file. It will cause a lot of time. So I just want to assigned this as a global value that I can use, but I keep get 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) //Here I got the data
}
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); //here it returns undefind
}
}
You cannot access the observable like that, you need to call the method and subscribe to the method as follows,
and in component,