# Angular

> Add image zoom to Angular with a standalone directive that attaches medium-zoom after render, updates its options from a signal input, and detaches on destroy.

Source: <https://medium-zoom.francoischalifour.com/docs/integrations/angular/>

medium-zoom works with Angular out of the box. Wrap it in a small directive, then add it to any `<img>`.

## Install the package

```sh
npm install medium-zoom
```

The standard import includes zoom styles and TypeScript definitions.

## Create the directive

Attach the zoom after Angular renders the image, update it when the options change, and detach it when the image is destroyed:

```angular
import { afterRenderEffect, DestroyRef, Directive, ElementRef, inject, input } from '@angular/core'
import mediumZoom, { type Zoom, type ZoomOptions } from 'medium-zoom'

@Directive({ selector: 'img[appZoomable]' })
export class ZoomableImage {
  readonly zoomOptions = input<ZoomOptions>({})

  constructor() {
    const image = inject<ElementRef<HTMLImageElement>>(ElementRef).nativeElement
    let zoom: Zoom | undefined

    afterRenderEffect(() => {
      const options = this.zoomOptions()
      if (zoom) {
        zoom.update(options)
      } else {
        zoom = mediumZoom(image, options)
      }
    })

    inject(DestroyRef).onDestroy(() => zoom?.detach())
  }
}
```

Save it as `zoomable-image.ts`. The directive is standalone and attaches with `afterRenderEffect()`, which requires Angular 20 or later.

## Display an image

Import the directive in your component and add `appZoomable` to the image:

```angular
import { Component } from '@angular/core'

import { ZoomableImage } from './zoomable-image'

@Component({
  selector: 'app-article',
  imports: [ZoomableImage],
  template: `
    <img
      appZoomable
      src="/assets/images/ocean-sunset.jpg"
      alt="Ocean sunset"
      width="1920"
      height="1280"
    />
  `,
})
export class Article {}
```

Click the image to enlarge it:

![Ocean sunset](https://medium-zoom.francoischalifour.com/assets/images/ocean-sunset.jpg)

Click the enlarged image or its background, press `Esc`, or scroll to close the zoom.

Width and height reserve space while the image loads. To keep the image responsive, follow the [image size step](https://medium-zoom.francoischalifour.com/docs/javascript/#set-the-image-size) of the JavaScript quickstart.

## Set options

Bind [options](https://medium-zoom.francoischalifour.com/docs/reference/javascript/options/) to change the margin, background, or scroll behavior:

```html
<img
  appZoomable
  [zoomOptions]="{ margin: 24, background: '#000000' }"
  src="/assets/images/ocean-sunset.jpg"
  alt="Ocean sunset"
/>
```

When the options change, the directive merges them into the zoom.

## Server rendering

Render effects run only in the browser, so the directive also works with Angular SSR and prerendering. The server outputs the plain image, and the zoom attaches after hydration.

## Next steps

- [HD images](https://medium-zoom.francoischalifour.com/docs/guides/hd-images/): Load a larger image when zooming.
- [Multiple images](https://medium-zoom.francoischalifour.com/docs/guides/multiple-images/): Attach zoom behavior to several images.
- [Styling](https://medium-zoom.francoischalifour.com/docs/guides/styling/): Set the layout, captions, and zoom background.
- [Options](https://medium-zoom.francoischalifour.com/docs/reference/javascript/options/): Set margins, backgrounds, scrolling, and custom layouts.
