I have an Angular 5 project.
I need to display images in the UI that are fetched from the back end (say for example I want to display users' avatars in the upper toolbar, like in the SO toolbar above).
The back end is a REST server that serves images when invoked on some end-point.
I cannot seem to GET the images from the back end. And I am sure that the back end works.
I have try to do the following (based on this article):
Here is my service image.service.ts
(boiler plate has been removed)
getImage(url: string): Observable<Blob> {
return this.httpClient.get(`${url}`, {responseType: 'blob'})
}
Here is my coponent image.component.ts
@Component({
selector: 'app-image',
templateUrl: './image.component.html',
styleUrls: ['./image.component.css']
})
export class ImageComponent implements OnInit {
@ViewChild('myImage') image: ElementRef
constructor(private imageService: ImageService) {}
ngOnInit() {
this.getMyImage('http://my-super-host:3000/images/super-hero.png')
}
getMyImage(url: string): void {
this.imageService.getImage(url)
.subscribe(response => this.image.nativeElement.src = window.URL.createObjectURL(response))
}
}
And eventually, here is my image.component.html
<img #myImage>
In the browser's console, there is an error message saying that
ERROR TypeError: _this.image is undefined
This solution doesn't work. What am I missing here?