javascript - Ant - Can't bind to 'ngModel' since it isn't a known property of 'nz-switch

I'm using Ant Design for Angular (ng-zorro-antd), and using the simple switch module to display a

I'm using Ant Design for Angular (ng-zorro-antd), and using the simple switch module to display a switch.

When I add the code per the docs:

<nz-switch [ngModel]="true"></nz-switch>

The app won't pile, and I get this error:

Can't bind to 'ngModel' since it isn't a known property of 'nz-switch'

I'm using Ant Design for Angular (ng-zorro-antd), and using the simple switch module to display a switch.

When I add the code per the docs:

<nz-switch [ngModel]="true"></nz-switch>

The app won't pile, and I get this error:

Can't bind to 'ngModel' since it isn't a known property of 'nz-switch'

Share Improve this question asked May 23, 2022 at 6:37 Tony BrasunasTony Brasunas 4,6313 gold badges47 silver badges61 bronze badges 3
  • try to import FormsModule into a module where declared this ponent – Yan Koshelev Commented May 23, 2022 at 7:19
  • You either forgot to import FormsModule in the parent module, or forgot to import the ponent(where you are using nz-switch) inside the module where nz-switch module is placed. – Muhammad Umar Commented May 23, 2022 at 8:58
  • Yes, thanks, that was it. The issue was that I was importing the FormsModule in a different "shared" module file. Once I moved it directly into the module where the switch is used I was good. – Tony Brasunas Commented May 23, 2022 at 19:26
Add a ment  | 

1 Answer 1

Reset to default 4

Import FormsModule in the right module

While it doesn't say it in the documentation, you need to have Angular's FormsModule imported in order for the ngModel binding to work.

If you have an app with just one module, this is easy. Simply import the FormsModule as below in your one and only xxx.module.ts file.

If you're working on an app with multiple modules, you have to ensure you're importing it in the module where you're going to use the nz-switch

In that module, where you're using the nz-switch, in the xxx.module.ts file:

import { FormsModule } from '@angular/forms`;

... 

@NgModule({
  declaration: [ ... ],
  imports: [ 
    ...,
    FormsModule
  ],
  ...

You should be good to go.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744955074a4603145.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信