【问题标题】:Can't bind to [(ngModel)] since it is not provided by any applicable directives无法绑定到 [(ngModel)],因为它不是由任何适用的指令提供的
【发布时间】:2021-07-31 01:58:46
【问题描述】:

我刚开始学习 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)我收到此错误:

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

P.P.S 在运行 ng serve 命令之前,我注意到 IDE 运行良好。

【问题讨论】:

  • 这是你的 IDE 的问题。我认为你不必担心这一点。
  • 我在使用 Intellij 并升级到 IntelliJ IDEA 2021.3.2(终极版)。问题消失了。

标签: angular webstorm


【解决方案1】:

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

【讨论】:

  • 我有最新版本的 webstorm,但重新安装旧版本的 angular 对我有帮助(从 12 到 11)
猜你喜欢
  • 2017-12-19
  • 2016-12-17
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-07-21
  • 2021-02-17
相关资源
最近更新 更多