Title: Allow for the ngSrc (and other inputs to change) when using NgOptimizedDirective · Issue #47813 · angular/angular · GitHub
Open Graph Title: Allow for the ngSrc (and other inputs to change) when using NgOptimizedDirective · Issue #47813 · angular/angular
X Title: Allow for the ngSrc (and other inputs to change) when using NgOptimizedDirective · Issue #47813 · angular/angular
Description: 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 using NgOptimizedImage (the directive) The directiv...
Open Graph Description: 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 using NgOpt...
X Description: 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 using NgOpt...
Opengraph URL: https://github.com/angular/angular/issues/47813
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow for the ngSrc (and other inputs to change) when using NgOptimizedDirective","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\r\n\r\ncommon\r\n\r\n### Description\r\n\r\nCurrently an error is thrown (in dev mode) when one of the inputs changes on an `img` when when using `NgOptimizedImage` (the directive)\r\n\r\nThe 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:\r\n\r\n\r\n\r\n\r\n\r\nA 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)\r\n\r\nCurrently did does not work as inputs are not allowed to change.\r\n\r\nOur 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.\r\n\r\n\r\n\r\n\r\n### Proposed solution\r\n\r\nAllow 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).\r\n\r\nIn `ngOnChanges` instead of throwing an error (we may still want to throw an error for some, like `priority`) determine what attributes on the `img` tag changed and need to be updated (or just update them all by calling `setHostAttributes()` again.\r\n\r\n### Alternatives considered\r\n\r\n Existing solutions to this are a little hacky and annoying to do but are as such:\r\n\r\n1. Create a directive that manually (with some type coercing) force the directive to rerender (THIS IS HACKY DO NOT DO THIS):\r\nhttps://angular-team.slack.com/archives/C08M4JKNH/p1666193540352369?thread_ts=1666128103.036069&cid=C08M4JKNH\r\n\r\n```typescript\r\nimport { NgOptimizedImage } from '@angular/common';\r\nimport type { OnChanges, SimpleChanges } from '@angular/core';\r\nimport { Directive, inject, Input } from '@angular/core';\r\n\r\n@Directive({\r\n selector: 'img[imageChange]',\r\n standalone: true,\r\n})\r\nexport class OptimizedImageChangedDirective implements OnChanges {\r\n private readonly ngOptimizedImage = inject(NgOptimizedImage);\r\n\r\n @Input() rawSrc!: string;\r\n\r\n ngOnChanges(changes: SimpleChanges): void {\r\n if (changes['rawSrc'] && !changes['rawSrc'].isFirstChange()) {\r\n (this.ngOptimizedImage as any).setHostAttributes();\r\n }\r\n }\r\n}\r\n```\r\n\r\n```angular2html\r\n\u003cimg [ngSrc]=\"media\" imageChange ...other attrs>\r\n```\r\n\r\n2. Wrapping in an `\u003cng-template>` and create an embedded view (as per @AndrewKushnir):\r\nhttps://angular-team.slack.com/archives/C08M4JKNH/p1666132985022759?thread_ts=1666128103.036069&cid=C08M4JKNH\r\n\r\n`You may try wrapping an \u003cimg> into \u003cng-template> in this case and create a view dynamically (via ViewContainerRef.createEmbeddedView) - this would re-create the view containing an \u003cimg> and you can control when this view should be re-created.`","author":{"url":"https://github.com/yharaskrik","@type":"Person","name":"yharaskrik"},"datePublished":"2022-10-19T15:57:28Z","url":"https://github.com/angular/angular/issues/47813"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:268e9ed0-1134-627a-06f1-cbb2c04a7d1e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9BC8:3A73AA:10D60AD:15471FD:6B015115 |
| html-safe-nonce | 97b5efab1d6ffaa3be6088a755a7f3b2360229e6efc106a0a2cb7204e62b9f05 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QkM4OjNBNzNBQToxMEQ2MEFEOjE1NDcxRkQ6NkIwMTUxMTUiLCJ2aXNpdG9yX2lkIjoiMzM1OTg2MTUzNzczMTg1ODcwOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 327318703a696df6d7de273696e1a4aca3f62ca653004a8d726e3ad892fa14bd |
| hovercard-subject-tag | issue:1415232356 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/47813/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fc2d06222fa27eeed9b74c1730ce39b0bf251f348fa0bf046e90c4fc128733e9/angular/angular/issues/47813 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fc2d06222fa27eeed9b74c1730ce39b0bf251f348fa0bf046e90c4fc128733e9/angular/angular/issues/47813 |
| og:image:alt | 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 using NgOpt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yharaskrik |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width