You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 18双向数据绑定报错:无法绑定textarea的ngModel属性

解决Angular 18独立组件中[(ngModel)]绑定报错问题

你遇到的“Can't bind to 'ngModel' since it isn't a known property of 'textarea'”错误,核心原因是独立组件的依赖隔离特性:在Angular独立组件模式下,每个组件必须自行导入所需的模块,父组件导入的模块不会自动传递给子组件。

你之前仅在app.component.ts中导入了FormsModule,但实际使用[(ngModel)]的PostCreateComponent并没有导入该模块,导致Angular无法识别ngModel指令。

修改步骤

打开post-create.component.ts,添加FormsModule的导入和声明:

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms'; // 导入FormsModule

@Component({
  selector: 'app-post-create',
  standalone: true,
  imports: [FormsModule], // 将FormsModule加入组件的imports数组
  templateUrl: './post-create.component.html',
  styleUrl: './post-create.component.css'
})
export class PostCreateComponent {
newPost="NO content Here";
enteredValue='';
onSavePost(){
  this.newPost = this.enteredValue;
}
}

修改后,PostCreateComponent就能正确识别[(ngModel)]指令,双向绑定即可正常工作。

内容的提问来源于stack exchange,提问作者priyanka chaudhari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 01:52:06