在html表单中使用ngForm,但出现错误
我已经创建了一个名为login的组件,并创建了一个html表单login.component.html,然后我想将这个表单作为角形表单。因此,我在login.component.html中添加了在html表单中使用ngForm,但出现错误,html,angular,Html,Angular,我已经创建了一个名为login的组件,并创建了一个html表单login.component.html,然后我想将这个表单作为角形表单。因此,我在login.component.html中添加了,并导入 import {NgForm} from '@angular/forms' 在login.component.ts中。你能帮我解决问题吗?我已经试过了 import { FormsModule } from '@angular/forms'; 但仍然不起作用 因此,当我运行服务器时,会
,并导入
import {NgForm} from '@angular/forms'
在login.component.ts中。你能帮我解决问题吗?我已经试过了
import { FormsModule } from '@angular/forms';
但仍然不起作用
因此,当我运行服务器时,会出现一个错误,上面说:
ERROR in src/app/login/login.component.html:2:23 - error NG8003: No directive found with exportAs 'ngForm'.
2 <form #loginform="ngForm">
~~~~~~
src/app/login/login.component.ts:6:16
6 templateUrl: './login.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component LoginComponent.
login.component.html:
<div class="container">
<form #loginform="ngForm">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox" name="remember"> Remember me</label>
</div>
<button type="submit" class="btn btn-default">Login</button>
</form>
</div>
电邮:
密码:
记得我吗
登录
使用-从'@angular/forms'导入{FormsModule,ReactiveFormsModule}
在module.ts中您是否在app.module.ts中导入了{FormsModule}
import {FormsModule, ReactiveFormsModule} from '@angular/forms'
@NgModule({
imports: [FormsModule]})
如果需要访问NGModel和模板驱动的表单,则需要导入FormsModule。
对于被动表单,我们需要在app.module.ts文件中导入被动表单模块
你可以在youtube上查看我的新角度课程,它涵盖了所有角度主题。您也可以在我的频道中找到反应式表单和模板表单。如果有兴趣,请看一看并支持我
从“@angular/forms”中删除
导入{NgForm}
和include从'@angular/forms'导入{FormsModule}代码>在最近的模块文件中,我从'@angular/forms'导入导入{FormsModule};在login.component.ts中,但仍然得到相同的错误。您是否也将其添加到模块中的导入数组中?你能添加一个复制你的问题吗?请检查此固定版本。我在imports数组中添加了FormsModule
import {FormsModule, ReactiveFormsModule} from '@angular/forms'
@NgModule({
imports: [FormsModule]})