首页 新闻 会员 周边 捐助

angular standalone component 中使用 ngModel 的问题

0
悬赏园豆:30 [已解决问题] 解决于 2024-12-30 08:23

将一个 component 转换为 standalone component

<div>
    <cnb-spinner [show]="isLoading">
        <cnb-editors-selector [(ngModel)]="defaultEditor"></cnb-editors-selector>
    </cnb-spinner>
</div>

[(ngModel)] 报错:

Can't bind to 'ngModel' since it isn't a known property of 'cnb-editors-selector'.

imports 中添加 NgModel

import { NgModel } from '@angular/forms';
@Component({
    imports: [NgModel]
})

却报错不支持 import NgModel

The directive 'NgModel' appears in 'imports', but is not standalone and cannot be imported directly. It must be imported via an NgModule.

请问如何解决这个问题?

dudu的主页 dudu | 高人七级 | 园豆:28699
提问于:2024-12-30 08:00
< >
分享
最佳答案
0

改为 FormsModule 就可以了

import { FormsModule } from '@angular/forms';
@Component({
    imports: [FormsModule]
})
dudu | 高人七级 |园豆:28699 | 2024-12-30 08:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册