0

I want to display the images stored in database. The database stores the path of the image. I want to retrieve the image using angular. How can i do that?

1
  • 1
    You have to write api and select query from that table and return as json and call it using http module from angular Commented Apr 29, 2019 at 11:20

1 Answer 1

1

You need create a service to call API to get image URL ( i assume image stored in JSON file)

   @Injectable()
    export class ConfigService {

      constructor(private http: HttpClient) { }

      getData() {
        return this.http.get('/assets/config.json');
      }
    }

Create component ts file to call service and return image url

export class AppComponent {
  name = 'Test display image';
  thumbnail: any;
  constructor(private imageService: ConfigService, private sanitizer: DomSanitizer) { }

  ngOnInit() {
     this.imageService.getData()
      .subscribe((baseImage : any) => {

        let objectURL = baseImage.image;

         this.thumbnail = objectURL;

      });
  }
}

Update HTML file to display image

<img width="100%" id="myimage" [src]='thumbnail' />

Demo: https://stackblitz.com/edit/display-imagepath-from-api

Sign up to request clarification or add additional context in comments.

2 Comments

Simple and Nice solution.
In your image, the url is absolute path, you should use relative path like /image/abc.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.