0

With this template

  <img  src="{{someDynamicImageSrcVar}}" 
  (load)="onImageLoad($event)"
  [ngStyle]="getImageStyle()"                
  >

After the image is changed and loads, onImageLoad() updates some properties.

getImageStyle() {
  const style = {
    'width.px': this.naturalWidth,
    'height.px': this.naturalHeight
  };
  console.log(style);
  return style;
}

The framework calls getImageStyle() which returns a style like

{width: "500px", height: "400px"}

However, the DOM is not updated apart from the first time getImageStyle() was called.

The style never changes from the initial values.

What should I do to make the style update?

intotecho
  • 4,925
  • 3
  • 39
  • 54

3 Answers3

1

The syntax for ngStyle is this

[ngStyle]="{'width.px':500, 'height.px': 500}"

So change your return object to this structure

{ 'width.px' : 500, 'height.px' : 500}
Akash Srivastav
  • 756
  • 5
  • 15
  • Good suggestion. I tried it but it had no effect. Now the style returned is {width.px: 400, height.px: 300}. But the element is not updated. – intotecho Jun 27 '19 at 11:57
1

ngStyle is work like this

[ngStyle]="{'color': 'blue', 'font-size': '24px', 'font-weight': 'bold'}"

Pavan Nagadiya
  • 652
  • 4
  • 10
0

the code is looks good! if you really returned {width: "500px", height: "400px"} i think you should return the value with single qutation like {width: '500px', height: '400px'}

I hope to help more :)

Rabea AlTaradeh
  • 213
  • 1
  • 9
  • Another good suggestion. I was already using single quotes, but when the console prints the style, it formats double quotes. – intotecho Jun 27 '19 at 11:58
  • try this answer [force-a-components-re-rendering](https://stackoverflow.com/questions/35105374/how-to-force-a-components-re-rendering-in-angular-2) to re-render the page after changes – Rabea AlTaradeh Jun 27 '19 at 13:27