无法绑定到 ‘ngModel’,因为它不是 ‘input’ 的已知属性 Angular

huangapple go评论62阅读模式
英文:

Can't bind to 'ngModel' since it isn't a known property of 'input' Angular

问题

以下是翻译好的部分:

我正在创建 Angular 表单设计时,创建过程中出现了错误 **无法绑定到 'ngModel',因为它不是 'input' 的已知属性**。我不知道为什么,我已经成功导入了 formsmodule,为什么会显示这个错误。我尝试了以下方法,如下所示。

    <form>
        <div class="form-group">
          <label>学生姓名</label>
          <input type="text" [(ngModel)]="name" [ngModelOptions]="{standalone: true}" class="form-control" id="name" placeholder="输入姓名">
        </div>

        <div class="form-group">
            <label>学生地址</label>
            <input type="text" class="form-control" [(ngModel)]="address" [ngModelOptions]="{standalone: true}" id="address" placeholder="输入地址">
          </div>

          <div class="form-group">
            <label>学生电话</label>
            <input type="text" class="form-control" [(ngModel)]="phone" [ngModelOptions]="{standalone: true}" id="phone" placeholder="输入电话">
          </div>

        <button type="submit" (click)="save()" class="btn btn-primary">提交</button>
      </form>


**app.module.ts**

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


    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';

    import { HttpClientModule } from '@angular/common/http';

    import { ReactiveFormsModule } from '@angular/forms';
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        ReactiveFormsModule ,
        HttpClientModule,

       
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
英文:

i am creating the angular form design while i creating i got the error was Can't bind to 'ngModel' since it isn't a known property of 'input' . i don't why i have import the formsmodule successfully why show us the error. what i tried so far i attached below.

&lt;form&gt;
    &lt;div class=&quot;form-group&quot;&gt;
      &lt;label&gt;Student Name&lt;/label&gt;
      &lt;input type=&quot;text&quot; [(ngModel)]=&quot;name&quot; [ngModelOptions]=&quot;{standalone: true}&quot;    class=&quot;form-control&quot;  id=&quot;name&quot; placeholder=&quot;Enter Name&quot; &gt; 
    &lt;/div&gt;

    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label&gt;Student Address&lt;/label&gt;
        &lt;input type=&quot;text&quot; class=&quot;form-control&quot;  [(ngModel)]=&quot;address&quot; [ngModelOptions]=&quot;{standalone: true}&quot; id=&quot;address&quot; placeholder=&quot;Enter Address&quot;&gt; 
      &lt;/div&gt;

      &lt;div class=&quot;form-group&quot;&gt;
        &lt;label&gt;Student Phone&lt;/label&gt;
        &lt;input type=&quot;text&quot; class=&quot;form-control&quot; [(ngModel)]=&quot;phone&quot; [ngModelOptions]=&quot;{standalone: true}&quot; id=&quot;phone&quot; placeholder=&quot;Enter Phone&quot;&gt; 
      &lt;/div&gt;

    &lt;button type=&quot;submit&quot; (click)=&quot;save()&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
  &lt;/form&gt;

app.module.ts

import { NgModule } from &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;


import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;

import { HttpClientModule } from &#39;@angular/common/http&#39;;

import { ReactiveFormsModule } from &#39;@angular/forms&#39;;
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule ,
    HttpClientModule,

   
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

答案1

得分: 1

导入FormsModule也在AppModule文件中

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { HttpClientModule } from '@angular/common/http';

import { ReactiveFormsModule, FormsModule } from '@angular/forms';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule,
    FormsModule,
    HttpClientModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
英文:

Import FormsModule also in AppModule file

import { NgModule } from &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;


import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;

import { HttpClientModule } from &#39;@angular/common/http&#39;;

import { ReactiveFormsModule,FormsModule } from &#39;@angular/forms&#39;;
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule ,
    FormsModule,
    HttpClientModule,

   
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

huangapple
  • 本文由 发表于 2023年6月22日 17:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定