Skip to content

docs: add new image directive snippet #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/pages/snippets/v15-new-image-directive.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: New Image Directive in Angular v15
description: "Example of New Image Directive in Angular 15 "
tags: ["angular15", "image directive"] # comma separated list which gets trimmed
pubDate: Mar 5, 2023
contributedBy: "@deepakrudrapaul"
---

Here is a simple example of how to use the _NgOptimizedImage_ directive.
The _NgOptimizedImage_ directive makes it easy to adopt performance best practices for loading images.

### Import NgOptimizedImage

```typescript
import { NgOptimizedImage } from "@angular/common";
```

### Enable the directive

To enable the NgOptimizedImage directive, replace your image's src attribute with _ngSrc_ and to prevent image related layout shifts,
NgOptimizedImage requires that you specify a height and width for the image

```html
<img ngSrc="image.jpg" width="300" height="200" />
```

And if you don't know the size of the images, you can use the _fill_ attribute to inherit the size of the parent container.
For the "fill" image to render properly, its parent element must be styled with
`position: "relative", position: "fixed", or position: "absolute".`

```html
<img ngSrc="image.jpg" fill />
```

---