当前位置: 首页 > 知识库问答 >
问题:

获取错误:无法绑定到“ng model”,因为它不是“input”的已知属性,尽管导入了FormsModule[duplicate]

梅玉堂
2023-03-14

我已经在app.module.ts中导入了FormsModule,但仍然会出现这个错误。

无法绑定到“ng model”,因为它不是“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}}

共有1个答案

弓方伟
2023-03-14

这样试试:

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}}
 类似资料: