这不是复制品.我只发现了另外一个问题,这个问题仍然没有得到回答.

无法绑定到"formControl",因为它不是"Input"的已知属性.我直接从Angular 文档复制了这段代码,并将其放入一个新的组件中.

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

@Component({
  selector: 'app-reactive-favorite-color',
  template: `
  Favorite Color: <input type="text" [formControl]="favoriteColorControl">
`
})
export class FavoriteColorComponent {
  favoriteColorControl = new FormControl('');
}

这是我的模块

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FavoriteColorComponent } from './favorite-color/favorite-color.component';

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

我补充道

import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';

发送到模块以try 解决此问题.具体的问题是第[formControl]="favoriteColorControl"行.我猜HTML标记input不知道[formControl]是什么.

这也不是我的VS代码的问题.我已经try 进入命令pallete并重新启动ANGLE语言服务器以及重新启动VS代码.

推荐答案

我在AppModule.ts文件的导入数组中没有看到FormsModuleReactiveFormsModule.try 将Reactive Forms模块和Forms模块添加到应用程序模块文件的Imports数组中.请参考下面的示例代码:

import {FormsModule, ReactiveFormsModule} from '@angular/forms';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule, // Add forms module here.
    ReactiveFormsModule // Add the reactive forms module here.
  ],
  ...
})
export class AppModule {}

Angular相关问答推荐

如何在Angular模块中的forRoot中注入配置

如何在自定义验证器中获取所有表单控件(字段组动态创建)

Angularmaterial 中的处理mat—radio—button表单值访问器

Angular 17@在大小写时切换多个值

带有搜索功能的CDK-VIRTUAL-SCROLL-VIEPORT不显示该值

使选项卡与父零部件成一定Angular 激活

如何防止变量值重置?

将ngModel绑定到@Input属性是不是一种糟糕的做法?

ION标签-从子对象中加载页,保留父对象的S内容

基于RxJS的Angular 服务数据缓存

Angular 12 区域环境切换器

http 调用的 RxJs 数组

PrimeNG:如何以编程方式更改分页器中的选定页面?

ng 生成组件不会创建 ngOnInit 和构造函数

如何在 Angular 4 中获取 HttpClient 状态码

单选按钮的Angular2 Reactive Forms formControl

找不到模块'webpack'

类型错误:无法读取未定义的属性

Angular Material2 主题 - 如何设置应用程序背景?

将 [NgStyle] 与条件组合(if..else)