Open
Description
Test Case:
Cause:
ngSrc
does not allow updating the input dynamically. An error is displayed in the console:
caught Error: NG02953: The NgOptimizedImage directive (activated on an <img> element with the `ngSrc="/nsxnMbkl9Lpc12AmYwKi9SmsJ7e.jpg"`) has detected that `ngSrc` was updated after initialization. The NgOptimizedImage directive will not react to this input change. Changing the `ngSrc` would have no effect on the underlying image element, because the resource loading has already occurred. To fix this, either switch `ngSrc` to a static value or wrap the image element in an *ngIf that is gated on the necessary value.
at postInitInputChangeError (common.mjs:6416:12)
at common.mjs:6435:19
at Array.forEach (<anonymous>)
at assertNoPostInitInputChange (common.mjs:6425:12)
at NgOptimizedImage.ngOnChanges (common.mjs:6143:13)
at NgOptimizedImage.rememberChangeHistoryAndInvokeOnChangesHook (core.mjs:2812:14)
at callHookInternal (core.mjs:3804:14)
at callHook (core.mjs:3835:9)
at callHooks (core.mjs:3786:17)
at executeCheckHooks (core.mjs:3717:5)
Solution:
There is an open issue angular/angular#47813
The alternatives are:
- Set the host attribute on the
NgOptimizedImage
again (see example in the angular issue above) - Recreate the view by using
<ng-template>
+<ng-container>
OR a directive like https://github.com/ngehlert/ngx-rerender/blob/main/projects/ngx-rerender/src/lib/ngx-rerender.directive.ts - Do not use
ngSrc
because it is for static image - Any other ideas?
I can make the PR if needed!