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.

Angular: ngModel/ngModelChange don't work in Storybook 6.2+

See original GitHub issue

What’s the problem? There was a way to define inputs for Angular components implementing ControlValueAccessor interface (reactive form controls) by using ngModel and ngModelChange as props in story. After upgrading to Storybook 6.2 these props seems do not work anymore.

  • new renderer: nothing happens, component.writeValue is not called
  • old renderer (angularLegacyRendering: true): see this error in console: TypeError: instance.writeValue is not a function, value is not passed to the component

Example:

Story for this use case exists in the demo, but does not work as well: https://next--storybookjs.netlify.app/angular-cli/?path=/story/basics-angular-forms-controlvalueaccessor--simple-input (currently works on Storybook 6.3.0-alpha.9)

Code for this story: https://github.com/storybookjs/storybook/blob/v6.3.0-alpha.9/examples/angular-cli/src/stories/basics/angular-forms/customControlValueAccessor/custom-cva-component.stories.ts

Is there documentation on this? ngModel and ngModelChange are not mentioned in documentation, so it’s more like undocumented (but implemented) feature.

Here is more details on how it is implemented in old renderer: https://github.com/storybookjs/storybook/blob/v6.2.8/app/angular/src/client/preview/angular/components/app.component.ts#L110

Additional context Would be good to understand if it’s a bug or there’s now another way to create stories for Angular form controls.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ThibaudAVcommented, Apr 27, 2021

🤔 arf, Bad behaviors of previous version which gives bad habits, I think 😁. The FormsModule was always loaded by storybook probably for some obscure reason.

As the new renderer behaves like a real angular application in this case if. So there are the same errors as if you have an angular application without this import FormsModule is an implementation detail. Not all stories need it, so it comes back to each story needing to “add it” I also think, the peerDependencies on @angular/forms should be deleted in apps/angular (with sb v7 ?)

the error message of angular is ~clear. But maybe a precision can be brought in the MIGRATION.md or the answer to this issue is enough 🤷‍♂️

1reaction
vetrovcommented, Apr 25, 2021

I can confirm this issue on storybook 11.2.9. In my console the following error shows up.

@daan93 Looks like you need to explicitly import FormsModule into your story. Do you have something like this in your code?

export default {
     title: 'Components/My Component,
     component: MyComponent,
     decorators: [
         moduleMetadata({
             imports: [
                 FormsModule
             ]
         })
     ]
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

problems with component using ControlValueAccessor #14627
I prepared example (below as Code Snippet) which seems to not work with Storybook - please use it to reproduce errors. Problems: Canvas...
Read more >
ControlValueAccessor ngModel not being updated
Now, why doesn't it work the other way around? When you're typing into the inner input, it will only invoke its onChange function, ......
Read more >
A brand new website interface for an even better experience!
Angular : ngModel/ngModelChange don't work in Storybook 6.2+.
Read more >
ngmodel change event target value Code Example
Answers related to “ngmodel change event target value”. angular input change ... button doesn't work while keyboard is shown react native ...
Read more >
48 answers on StackOverflow to the most popular Angular ...
What is the Angular equivalent to an AngularJS $watch? Importing lodash into angular2 + typescript application; How to detect a route change in ......
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