我在app.module.ts中导入了FormsModule,但我仍然收到此错误。
无法绑定到'ngModel',因为它不是'input'的已知属性
我已经完成了类似的帖子,但我仍然无法找到解决方案。
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {FormsModule} from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
import {FormsModule} from '@angular/forms';
import {NgModule } from "@angular/core";
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [
AppComponent
],
bootstrap: [AppComponent]
})
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
}
app.component.html
<input type=radio name="gender" value="Mr" [(ngModel)]="gender">Male
<input type=radio name="gender" value="Mrs" [(ngModel)]="gender">Female
{{"Hello " + gender}}
答案 0 :(得分:3)
试试这样:
<强> app.module.ts 强>
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
FormsModule,
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
<强> app.component.ts 强>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
gender: string;
}
<强> app.component.html 强>
<input type=radio name="gender" value="Mr" [(ngModel)]="gender">Male
<input type=radio name="gender" value="Mrs" [(ngModel)]="gender">Female
{{"Hello " + gender}}