You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(common): Add loaderParams attribute to NgOptimizedImage (#48907)
Add a new loaderParams attribute, which can be used to send arbitrary data to a custom loader, allowing for greater control of image CDN features.
PR Close#48907
Copy file name to clipboardExpand all lines: aio/content/guide/image-directive.md
+34-2Lines changed: 34 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -276,9 +276,41 @@ providers: [
276
276
],
277
277
</code-example>
278
278
279
-
A loader function for the `NgOptimizedImage` directive takes an object with the `ImageLoaderConfig` type (from `@angular/common`) as its argument and returns the absolute URL of the image asset. The `ImageLoaderConfig` object contains the `src` and `width` properties.
279
+
A loader function for the `NgOptimizedImage` directive takes an object with the `ImageLoaderConfig` type (from `@angular/common`) as its argument and returns the absolute URL of the image asset. The `ImageLoaderConfig` object contains the `src`property, and optional `width` and `loaderParams` properties.
280
280
281
-
Note: a custom loader must support requesting images at various widths in order for `ngSrcset` to work properly.
281
+
Note: even though the `width` property may not always be present, a custom loader must use it to support requesting images at various widths in order for `ngSrcset` to work properly.
282
+
283
+
### The `loaderParams` Property
284
+
285
+
There is an additional attribute supported by the `NgOptimizedImage` directive, called `loaderParams`, which is specifically designed to support the use of custom loaders. The `loaderParams` attribute take an object with any properties as a value, and does not do anything on its own. The data in `loaderParams` is added to the `ImageLoaderConfig` object passed to your custom loader, and can be used to control the behavior of the loader.
286
+
287
+
A common use for `loaderParams` is controlling advanced image CDN features.
288
+
289
+
### Example custom loader
290
+
291
+
The following shows an example of a custom loader function. This example function concatenates `src` and `width`, and uses `loaderParams` to control a custom CDN feature for rounded corners:
let url = `https://example.com/images/${config.src}?`;
296
+
let queryParams = [];
297
+
if (config.width) {
298
+
queryParams.push(`w=${config.width}`);
299
+
}
300
+
if (config.loaderParams?.roundedCorners) {
301
+
queryParams.push('mask=corners&corner-radius=5');
302
+
}
303
+
return url + queryParams.join('&');
304
+
};
305
+
</code-example>
306
+
307
+
Note that in the above example, we've invented the 'roundedCorners' property name to control a feature of our custom loader. We could then use this feature when creating an image, as follows:
0 commit comments