首页 > 解决方案 > 无法绑定到 [(ngModel)],因为它不是由任何适用的指令提供的

问题描述

我刚开始学习 Angular,想尝试实现 2 路绑定。

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import {FormsModule} from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'sample text';
}

app.component.html

<input type="text" [(ngModel)]="title">
<p>{{title}}</p>

此代码按应有的方式运行和工作,但由于某种原因在 IDE 中(我正在使用 WebStorm)我收到此错误:

在此处输入图像描述

PS 是的,我阅读了与此错误相关的主题,但所有答案都说只是导入 FormsModule,我还是这样做了。

PPS 我注意到在运行 ng serve 命令之前 IDE 运行良好。

标签: angularwebstorm

解决方案


在执行 Angular 版本更新(从 10->12)之后,我今天也遇到了 webstorm 的这个问题。Angular 12 使用较新的 Typescript 版本,我认为它在 IDE 中导致了此错误。更新到最新的 webstorm 版本为我修复了它。


推荐阅读