Allow for the ngSrc (and other inputs to change) when using NgOptimizedDirective #47813
Labels
area: common
common: image directive
feature: votes required
feature
Issue that requests a new feature
Milestone
yharaskrik commentedOct 19, 2022
•
edited
Which @angular/* package(s) are relevant/related to the feature request?
common
Description
Currently an error is thrown (in dev mode) when one of the inputs changes on an
img
when when usingNgOptimizedImage
(the directive)The directive currently only supports the most basic case of rendered once and only once, but there are many use cases where an image may change (realtime applications for example). Our use case in particular is a website builder that allows the user to edit the page inline (and eventually in realtime). Heres some screenshots from Trellis.org to demonstrate this:
A user can click to change the media which opens up a photo library using Unsplash and then crops the image, after the server response the
ngSrc
is updated with the new image (currently no other inputs change as the element is a static size)Currently did does not work as inputs are not allowed to change.
Our rendered live pages (the ones the users of our users end up seeing after the page is finished being built) use the directive already but it would be nice if our website builder itself could be using it as well.
Proposed solution
Allow for the Inputs to change and check and update either all of the attributes or only the ones that would be affected by that specific attribute(s).
In
ngOnChanges
instead of throwing an error (we may still want to throw an error for some, likepriority
) determine what attributes on theimg
tag changed and need to be updated (or just update them all by callingsetHostAttributes()
again.Alternatives considered
Existing solutions to this are a little hacky and annoying to do but are as such:
https://angular-team.slack.com/archives/C08M4JKNH/p1666193540352369?thread_ts=1666128103.036069&cid=C08M4JKNH
<ng-template>
and create an embedded view (as per @AndrewKushnir):https://angular-team.slack.com/archives/C08M4JKNH/p1666132985022759?thread_ts=1666128103.036069&cid=C08M4JKNH
You may try wrapping an <img> into <ng-template> in this case and create a view dynamically (via ViewContainerRef.createEmbeddedView) - this would re-create the view containing an <img> and you can control when this view should be re-created.
The text was updated successfully, but these errors were encountered: