I have a component which html structure look like:
<img mat-card-sm-image src="{{img}}" />
and in typescript
constructor(private loginService: LoginService) {
this.img = null;
this.loadImage();
}
loadImage = function () {
this.img = this.loginService.loginImg();
}
and login service:
loginImg() {
const url = "http://localhost:59078/api/image/login";
this.http.get(url, { responseType: 'blob' }).subscribe(result => {
console.log(result);
return result;
});
}
and the API Core controller
[HttpGet]
[AllowAnonymous]
[Produces("image/png")]
public IActionResult Login()
{
var file = Path.Combine(Directory.GetCurrentDirectory(),
"wwwroot", "images", "login.png");
return PhysicalFile(file, "image/png");
}
The idea that the image is not loaded... Why ?
the
return
here does nothing (especially since you aren't returning the http.get iteself). The Service should return the observable http.get (without the subscribe) and then when you subscribe to it from your component, you change theimg
variable from inside.Here's how you can fix it
this is unrelated, but you might consider using an Anugular lifecycle hook like
ngOnInit
instead of the constructor for initializing variables