question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use constructor inside a story

See original GitHub issue

Is it possible to inject a service inside a storybook using a contstructor or something else?

We are implementing storybook with our angular components. And it would be nice if we can do something like:

export const story = () => ({
    template: `
        <my-component (click)="clicked()"></my-component>
    `,
    constructor(private customService: MyCustomService): void {
    },
    props: {
        clicked(): void {
            this.customService.doSomething();
        }
    }
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:7

github_iconTop GitHub Comments

3reactions
dimitriy-kcommented, Oct 20, 2020

I have similar problem, I want to build FormGroup with FormBuilder, but cannot figure out how to do it for story, because there is no constructor, so I cannot inject FormBuilder, and where can I call method like:

this.form = this.formBuilder.group({
      input: ['', Validators.required],
      email: ['', Validators.email],
    });
0reactions
dimitriy-kcommented, Feb 25, 2021

Also trying to work this out. Did you find a way to do this?

I just build FormGroup:

import { FormGroup, FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';

export const myFormField = () => ({
  moduleMetadata: {
    imports: [
      ReactiveFormsModule,
      FormsModule,
    ],
  },
  component: MyFormFiledComponent,
  template: `
  <form [formGroup]="formGroup">
    <my-form-field formControlName="formControl"></my-form-field>
    <my-form-field formControlName="formControlPrefilled"></my-form-field>
  </form>
  `,
  props: {
    formGroup: new FormGroup({
      dropdown: new FormControl(null),
      formControlPrefilled: new FormControl({ start: 21, end: 345 }),
    }),
  },
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding Custom Constructors (How To) - Treehouse
then click on Code > Generate > Constructor, select both and click OK. 0:35. All right, so let's use these back in our...
Read more >
Constructor in Java | DigitalOcean
When a constructor calls another constructor of the same class, it's called constructor chaining. We have to use this keyword to call another ......
Read more >
Java Constructor Basics: What They Are & How to Use Them
In Java, a constructor is a term that describes a line of code used to create an instance of a class object. Since...
Read more >
How do I call one constructor from another in Java?
Instead of calling a constructor within a constructor, use the Factory pattern. A static factory method first creates all lower-level ...
Read more >
Constructors in Java - A Complete Guide - BeginnersBook
Java Copy Constructor · By creating a parameterized constructor with class reference as parameter. · By using clone() method of the Object class....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found