0

In my Angular 5 app, the user uploads images to the API, which responds with URL's to the new images. After the promise is completed, I'm able to add the new image objects to the DOM, but the images don't display in the GUI unless I reload the page.

Before page reload:

enter image description here

After page reload:

enter image description here

How can I make the images show without reloading the entire page?

Here's the promise:

    this.http.post(`${this.env.api}/v1/images`, formData)
        .map(res => res.json())
        .catch(error => Observable.throw(error))
        .subscribe(
            (response) => {
                for (let image of response.data) {
                    this.campaign.images.push(image);
                }
            },
        )

And here's the HTML:

<div *ngFor="let image of campaign.images; let i = index;">
    <a [href]="image.web_url" target="_blank">
        <img src="{{ image.web_url }}?width=100&height=100">
    </a>
</div>
americanknight
  • 689
  • 3
  • 18
  • 37
  • 1
    Pushing items into an array often causes trouble with change detection - try replacing the whole array with a new one containing the existing and new items, instead. See e.g. https://stackoverflow.com/q/47687441/3001761 for manual change detection. – jonrsharpe Jun 20 '18 at 16:32

1 Answers1

-1

Try the following:

this.http.post(`${this.env.api}/v1/images`, formData)
        .map(res => res.json())
        .catch(error => Observable.throw(error))
        .subscribe(
            (response) => {
                for (let image of response.data) {
                    let images = [...this.campaign.images];
                    images.push(image);
                    this.campaign.images = [...images];

                }
            },
        )