Using Angular services in Storybook 6

How to call Angular Injectable directly in Angular Stories by using a custom Storybook decorator

Jonathan Gelin
2 min readApr 8, 2022

This article is the new version of my previous article:

But when I am using Storybook for creating Angular stories, I want to be able to use Angular services directly in my story.

However, this is not possible without creating a wrapper component to inject the service.

To solve this issue, I created a custom Storybook decorator that is responsible for exposing the Angular Injector in my story props. Then I can access all services available in my Angular story scope.

You can find below the custom decorator used to solve this:

How this is working?

The custom decorator will set up an APP_INITIALIZER provider that will be executed when the Angular story will be bootstrapped.

It will then use the Angular Factory Provider approach to inject the global Injector service.

Then this Injector will be injected into the props of the story.

Then I can use it in the stories. A common use case would be to open a dialog directly in the story:

You can use this approach with all services that are available in your Angular story!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Jonathan Gelin
Jonathan Gelin

Written by Jonathan Gelin

Write & Share about SDLC/Architecture in Ts/Js, Nx, Angular, DevOps, XP, Micro Frontends, NodeJs, Cypress/Playwright, Storybook, Tailwind • Based in 🇪🇺

Responses (2)

Write a response

Works great for Storybook 6, unfortunatelly I updated to version 7 and injector is undefined..

Thank you very much for sharing good article.