【angular报错】组件设置standalone后:Can‘t bind to ‘ngModel‘ since it isn‘t a known property of ‘input‘.

背景

在练习路由的组件懒加载的时候,将组件设置为standalone,然后组件对应的html中使用的*ngModel报错:

Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’.

在这里插入图片描述
搜了很多解决方法,都是直接import导入FormsModule这个模块解决的。

题外话

在出现这个问题的同时,*ngFor也报警告了。合理推测都是由设置standalone后导致的问题。

The *ngFor directive was used in the template, but neither the NgFor directive nor the CommonModule was imported. Please make sure that either the NgFor directive or the CommonModule is included in the @Component.imports array of this component.

在这里插入图片描述
由报错内容可知,需要引入CommonModule这个模块。
由于组件已经被设置为standalone,而它本身并没有ngFor这种指令,需要从CommonModule中导入。

参考:这里

在这里插入图片描述
因此,解决方法为:

在这里插入图片描述
问题解决了。

解决方法

以此类推,解决方法为:在组件中引入对应指令的模块。

在这里插入图片描述

组件/模块 要用到的东西,要再次引入,而全局配置只需在根目录引入一次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 当出现"Can't bind to 'ngModel' since it isn't a known property of 'input'"错误时,这是因为没有在当前组件所属的模块中引用FormsModule。要解决这个问题,你需要在当前组件所属的模块中添加FormsModule的引用。具体做法是在module.ts文件中添加以下代码:import { FormsModule } from '@angular/forms';然后在imports数组中加入FormsModule。这样就可以解决"Can't bind to 'ngModel' since it isn't a known property of 'input'"错误了。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [error NG8002: Can‘t bind to ‘ngModel‘ since it isn‘t a known property of ‘input‘.](https://blog.csdn.net/qq_37860634/article/details/120401597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Can‘t bind to ‘ngModel‘ since it isn‘t a known property of ‘nz-range-picker‘.解决angular使用...](https://blog.csdn.net/qq_38594056/article/details/126465949)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [AngularngModel报错angular-can‘t-bind-to-‘ngModel‘---](https://blog.csdn.net/bobo789456123/article/details/117155567)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

karshey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值