I have simple component to test string interpolation. In html,
<p>{{value}}</p>
The ts file:
export class HelloComponent implements OnInit {
value: any;
constructor(private service: ApiService) {
this.value = 'test';
}
ngOnInit() {
this.getValue();
}
getValue = (): void => {
this.value = this.service.getValue();
}
Now since I injected the service so let's look at it.
@Injectable({
providedIn: 'root'
})
export class ApiService {
url: string;
value: any;
constructor(private http: HttpClient) {
this.url = 'http://localhost:8080/api/test';
}
getValue = () => {
this.http.get(this.url, {responseType: 'text'}).pipe(take(1)).subscribe(
data => {
this.value = data;
console.log(data); // 'Hello World' is printed out.
return data;
}
);
}
}
My question is that I can see the correct value in console log. But I can't get the value in the component. It is undefined value so on the screen it is nothing.