Skip to content
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

No providers for Angular's services (ChangeDetectorRef, Renderer, ElementRef) in Angular. #7544

Closed
izmaylovdev opened this issue Jul 24, 2019 · 17 comments
Assignees
Milestone

Comments

@izmaylovdev
Copy link

izmaylovdev commented Jul 24, 2019

Describe the bug
I try to create story for component which has a ChangeDetecorRef as dependency. As result I got an error Can't resolve all parameters for MyComponentName: (?).. If remove ChangeDetectorRef, all works well.

To Reproduce
Steps to reproduce the behavior:

  1. Create component with ChangeDetecorRef as dependency.
  2. Try to write story for the component.

Expected behavior
Story works well.

Screenshots
image

System:

  • OS: Windows10
  • Device: Dell Latitude
  • Browser: chrome
  • Framework: angular@8
  • Version: @storybook/angular": "^5.1.9"
@shilman shilman added this to the 5.1.x milestone Jul 24, 2019
@kroeder kroeder self-assigned this Jul 27, 2019
@kroeder
Copy link
Member

kroeder commented Jul 27, 2019

Can confirm this

@kroeder
Copy link
Member

kroeder commented Jul 27, 2019

doing it like that works though - weird

  constructor(@Inject(ChangeDetectorRef) private cdr: ChangeDetectorRef) {}

This might be caused by this: https://github.com/storybookjs/storybook/blob/next/app/angular/src/client/preview/angular/components/app.component.ts#L51

I've never used componentRef.injector.get before but maybe it's causing this unwanted behavior

@izmaylovdev
Copy link
Author

@kroeder

constructor(@Inject(ChangeDetectorRef) private cdr: ChangeDetectorRef) {}

In this case you don't get errors but this.cdr does not work.

@stale
Copy link

stale bot commented Aug 27, 2019

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Aug 27, 2019
@izmaylovdev
Copy link
Author

So sad...

@stale stale bot removed the inactive label Sep 2, 2019
@shilman shilman modified the milestones: 5.1.x, 5.2.x Sep 23, 2019
@stale
Copy link

stale bot commented Oct 14, 2019

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Oct 14, 2019
@shilman shilman added the todo label Oct 14, 2019
@stale stale bot removed the inactive label Oct 14, 2019
@izmaylovdev
Copy link
Author

As I can see, not only ChangeDetectorRef is missing in Injector, if I try to inject ElementRef or Renderer2, I get the same error... This also applies to nested components/directives.

@danielhdz56
Copy link

yeah any service i add in the constructor throws:
Can't resolve all parameters for CustomService: (?)

any way around this?

@izmaylovdev izmaylovdev changed the title No provider for ChangeDetectorRef in Angular. No providers for Angular's services (ChangeDetectorRef, Renderer, ElementRef) in Angular. Nov 1, 2019
@patrickjm
Copy link

patrickjm commented Nov 25, 2019

I'm having the same issue when I try to inject an ElementRef and Renderer2 into a directive's constructor. Based on the implementation, I can see why ElementRef especially would be difficult to inject, but it's also a really important part of Angular. Not sure where to go from here... I'm 40 stories/components in and just now finding out I can't write directives!

Edit: if it helps anyone, I was able to get around this issue using @HostBinding annotations on the directive for the properties I was originally trying to change on the ElementRef. Doesn't cover every case, though.

@patrickjm
Copy link

The solution to this is adding "emitDecoratorMetadata": true to your .storybook/tsconfig.json.

https://stackoverflow.com/a/57115788/2609014

@shilman shilman modified the milestones: 5.2.x, 5.3.x Jan 11, 2020
@rootdevelopper
Copy link

Hi:

I'm having the same issue, just like @danielhdz56, my story works just fine but as soon as I add a service to my component it breaks with the error:

Can't resolve all parameters for LoginComponent: (?, ?, ?).

I added "emitDecoratorMetadata": true, to .storybook/tsconfig.json and now I get the error: NullInjectorError: No provider for Router!

So I added a provider:

.addDecorator(
moduleMetadata({
declarations: [],
imports: [CommonModule, BrowserAnimationsModule, LoginModule],
providers: [Router, {provide: 'API_BASE_URL', useValue: 'http://localhost:8080/users'}]
})
)
and now, I'm getting the initial error again, but this time with the router:

Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?).

@storybook/angular v5.3.9

in angular:

@angular-devkit/architect 0.900.0-rc.10
@angular-devkit/build-angular 0.900.0-rc.10
@angular-devkit/build-optimizer 0.900.0-rc.10
@angular-devkit/build-webpack 0.900.0-rc.10
@angular-devkit/core 9.0.0-rc.10
@angular-devkit/schematics 9.0.0-rc.10
@angular/cdk 8.2.3
@angular/material 8.2.3
@angular/pwa 0.803.23
@ngtools/webpack 9.0.0-rc.10
@schematics/angular 9.0.0-rc.10
@schematics/update 0.900.0-rc.10
rxjs 6.5.4
typescript 3.7.5
webpack 4.41.2

@patrickjm
Copy link

@rootdevelopper You probably need to add the angular routing module to your decorator imports

@rootdevelopper
Copy link

thanks, @patrickjm, I tried that already, I forgot to mention.
A few weeks ago I wasn't able to run storybook in angular 9 RC, so I'll wait a few more weeks to see if this issue gets resolved on the next storybook release.

@kroeder
Copy link
Member

kroeder commented Feb 3, 2020

I've created a PR (#9701) that should fix this issue. It will add "emitDecoratorMetadata": true to Storybook without the need of adding it to .storybook/tsconfig.json

@shilman
Copy link
Member

shilman commented Feb 3, 2020

Ta-da!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.0.0-alpha.4 containing PR #9701 that references this issue. Upgrade today to try it out!

You can find this prerelease on the @next NPM tag.

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Feb 3, 2020
@shilman
Copy link
Member

shilman commented Feb 4, 2020

Yowza!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.3.11 containing PR #9701 that references this issue. Upgrade today to try it out!

@brezodani
Copy link

https://stackoverflow.com/a/57115788/2609014

This helped me, thanks. Just added to tsconfig.json the

"compilerOptions": {
      "emitDecoratorMetadata": true,
    },

I'm using 5.3.6, did not try the newer version @shilman mentioned above, but I'm guessing the PR #9701 solved that, cuz it does the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants