ngmodel和ngform在角度形式下根本不起作用



我知道以前有人问过这个问题,但所有的解决方案都是关于导入FormsModule&@angular/forms中的ReactiveFormsModule,但实际上我已经完成了这项工作,并将它们添加到导入中,但ngFormngModel中的任何内容都没有按预期工作。向我显示的错误是:

" error NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input' " &
" There is no directive with 'exportAs' set to 'ngForm' "

我已经尝试将Angular版本更改为早期版本,但仍然会出现同样的问题

这是我的进口

import { AddProductComponent } from './Features/products/add-product/add-product.component';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {RouterModule} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing';
import {FormsModule , ReactiveFormsModule} from '@angular/forms'
import { AppComponent } from './app.component';
import { ProductsComponent } from './Features/products/product.component';
import { ProductListingComponent } from './Features/products/product-listing/product-listing.component';
import { ProductItemComponent } from './Features/products/product-item/product-item.component';
@NgModule({
declarations: [
AppComponent,
ProductsComponent,
ProductListingComponent,
ProductItemComponent,
BtnComponent,
...........etc

],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
............ etc .

这是产生错误的HTML代码

<form #myform="ngForm" class="add-product" (ngSubmit)="onSubmit(myForm)">

它显示了一个错误";没有将"exportAs"设置为"ngForm"的指令">

还有这个代码(注意,产品是在我的组件中初始化的对象(

<div class="form-group">
<label for="">Name</label>
<input [(ngModel)]="product.Name" class="form-control" type="text" id="">
</div>

这显示了一个错误";错误NG8002:无法绑定到"ngModel",因为它不是"input"的已知属性">

如果我从代码中删除了这些(ngForm和ngModule(,整个代码运行得非常好。。。那么我能做什么

最后,我通过停止服务器并运行命令来解决问题:

npm update

然后在重新启动服务器之后,问题得到了解决。

在AppModule 中导入FormsModule

import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule      
]

最新更新