TypeScript- 的 Angular 框架错误 - "There is no directive with exportAs set to ngForm"
TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"
我在使用 TypeScript 的 Angular2-forms 框架时不断收到此错误:
There is no directive
with "exportAs" set to "ngForm"
这是我的代码
项目依赖:
"dependencies": {
"@angular/common": "2.0.0-rc.6",
"@angular/compiler": "2.0.0-rc.6",
"@angular/core": "2.0.0-rc.6",
"@angular/forms": "2.0.0-rc.6",
"@angular/http": "2.0.0-rc.6",
"@angular/platform-browser": "2.0.0-rc.6",
"@angular/platform-browser-dynamic": "2.0.0-rc.6",
"@angular/router": "3.0.0-rc.2",
"ng2-bootstrap": "^1.1.1",
"reflect-metadata": "^0.1.8",
"core-js": "^2.4.0",
"es6-module-loader": "^0.17.8",
"rxjs": "5.0.0-beta.11",
"systemjs": "0.19.27",
"zone.js": "0.6.17",
"jquery": "3.0.0",
}
这是登录模板:
<form #loginForm="ngForm" (ng-submit)="authenticate(loginForm.value)">
</form>
...和登录组件:
import { Component } from '@angular/core';
import {Http, Headers} from '@angular/http';
@Component({
moduleId: module.id,
selector: 'login-cmp',
templateUrl: 'login.component.html'
})
export class LoginComponent {
constructor($http: Http) {
this.$http = $http;
}
authenticate(data) {
...
}
}
我有这个错误:
zone.js?1474211973422:484 Unhandled Promise rejection: Template parse errors:
There is no directive with "exportAs" set to "ngForm" ("
<form [ERROR ->]#loginForm="ngForm"
(ngsubmit)="authenticate(loginForm.value)">
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [FormsModule],
...
})
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
FormsModule //<----------make sure you have added this.
],
....
})
检查是否导入了FormsModule。新表单 angular 2 发行版中没有 ngControl。您必须将模板更改为示例
<div class="row">
<div class="form-group col-sm-7 col-md-5">
<label for="name">Name</label>
<input type="text" class="form-control" required
[(ngModel)]="user.name"
name="name" #name="ngModel">
<div [hidden]="name.valid || name.pristine" class="alert alert-danger">
Name is required
</div>
</div>
</div>
您不仅必须将 FormsModule
导入到根 AppModule 中,而且还必须导入到使用任何 angular 表单指令的每个子模块中。
// SubModule A
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
CommonModule,
FormsModule //<----------make sure you have added this.
],
....
})
(以防别人跟我一样瞎)
form
FTW!确保使用 <form>
标签
不会工作:
<div (ngSubmit)="search()" #f="ngForm" class="input-group">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Go!</button>
</span>
<input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search...">
</div>
魅力十足:
<form (ngSubmit)="search()" #f="ngForm" class="input-group">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Go!</button>
</span>
<input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search...">
</form>
你必须关心的三件事:
如果您使用的是子模块,则必须在该子模块中导入 FormModule
:
imports:[CommonModule, HttpModule, FormsModule]
您必须导出模块中的 FormModule
:
exports:[FormsModule]
所以放在一起看起来像:
imports:[CommonModule, HttpModule, FormsModule],
exports:[FormsModule],
在文件的顶部你必须导入 FormsModule
:
import {FormsModule} from '@angular/forms';
如果您只使用 app.module.ts
则无需导出。只需要导入。
我在 imports[]
数组中添加了 ReactiveFormsModule
来解决这个错误。
错误:没有将“exportAs”设置为“ngForm”的指令(“
修复:
module.ts:
import {FormsModule, ReactiveFormsModule} from '@angular/forms'
imports: [
BrowserModule,
FormsModule ,
ReactiveFormsModule
],
登录组件ts文件中除了导入form模块外还需要导入NgForm
import { NgForm } from '@angular/forms';
这解决了我的问题
如果您已经导入 FormsModule
,那么您只需从输入字段中删除 formControlName='whatever'
。
你应该用 ctrl+c 终止应用程序并用 ng serve 重新 运行 它,如果你没有将 FormsModule 包含到你 app.module 文件导入数组,然后稍后添加它,angular 不知道,它没有 re-scan 模块,你应该重新启动应用程序,这样 angular 可以看到包含了新模块,之后它将包含模板驱动方法的所有功能
import { FormsModule,ReactiveFormsModule }from'@angular/forms';
imports:[
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
FormsModule,
ReactiveFormsModule/*This one in particular*/,...
],
in app.module.ts 以永久解决 "cannot bind [formGroup] or no directive with export as"
.
等错误
如果这样分配名称:
#editForm="testForm"
...必须在组件装饰器中定义 exportAs:
selector: 'test-form',
templateUrl: './test-form.component.html',
styleUrls: ['./test-form.component.scss'],
exportAs: 'testForm'
在我的例子中,我不得不从我的 <form>
标签中删除 ngNoForm
属性。
If you you want to import FormsModule in your application but want to skip a specific form, you can use the ngNoForm directive which will prevent ngForm from being added to the form
参考:https://www.techiediaries.com/angular-ngform-ngnoform-template-reference-variable/
这个
<div #myForm="ngForm"></div>
也会导致错误,可以通过包含 ngForm directive.
来修复
<div ngForm #myForm="ngForm"></div>
我刚刚移动了路由模块,即 ARoutingModule 在 FormsModule 和 ReactiveFormsModule 之上,在 CommonModule 之后,在导入的模块数组中。
出于同样的原因,我一遍又一遍地问过同样的问题。因此,让我通过说出我做错了什么来回答这个问题。可能对某人有帮助。
我正在通过 angular-cli
命令
创建组件
ng g c components/something/something
它所做的,是创建我想要的组件。
此外,在创建组件时,它在应用程序模块的声明数组中添加了组件。
如果是这种情况,请将其删除。
瞧!它可能会起作用。
如果您尝试使用 jasmine 在 angular 中编写单元测试用例,也会发生此错误。
这个错误的基本概念是import FormsModule
。因此,在单元测试文件中,我们添加 imports Section 并将 FormsModule 放在该文件中
下
TestBed.configureTestingModule
For eg:
TestBed.configureTestingModule({
declarations: [ XYZComponent ],
**imports: [FormsModule]**,
}).compileComponents();
在我的例子中,我忘记将我的组件添加到 app.module.ts 的声明数组中,瞧!问题已解决。
检查另一项:
确保您的组件已添加到 app.module.ts
中 @NgModule 的声明数组
@NgModule({
declarations: [
YourComponent,
],
当运行 ng generate component
命令时,它不会自动将其添加到app.module。
是的,我遇到了同样的问题...我做了最重要的事情,但没有成功。
这个解决了我的问题。因为我们在严格模式下使用 angular 'with'
在app.module.ts中添加这段代码
import {FormsModule, ReactiveFormsModule} from '@angular/forms'
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
],
以及您使用的是哪个文件 NgForm
在 .html 文件中
<form #form="ngForm" (ngSubmit)="loginSubmit(form);">
</form>
并在 .ts 文件中
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-login-page',
templateUrl: './login-page.component.html',
styleUrls: ['./login-page.component.scss']
})
export class LoginPageComponent implements OnInit {
NgForm=NgForm;//this one solve my problem...initialization and declaration
}
您必须声明 FormsModule 并且还必须声明 Component
declarations: [
YourComponent -->
],
imports: [
BrowserModule,
FormsModule, -->
],
同意@micronyks 提供的解决方案,但如果您的应用程序很简单且其中没有很多模块,则该解决方案适用。否则,如果您不想将它暴露给所有模块或整个应用程序,我们需要以类似的方式添加到我们使用
几乎所有的答案都在谈论添加 FormsModule
模块,但在我的例子中,导入已经完成。正如在对问题的回答中所说,您必须关闭并重新启动 VScode 或者在我的情况下,您必须使用“将工作区另存为...”来保存您的工作区,然后一切都会再次正常工作。
.HTML file <form (ngSubmit)="addIngredients(f)" #f="ngForm">
.ts file @ViewChild('f', { static: false }) slForm: NgForm;
在 HTML 中将 ngForm 更新为对我有用的 NgForm
我在使用 TypeScript 的 Angular2-forms 框架时不断收到此错误:
There is no
directive
with "exportAs" set to "ngForm"
这是我的代码
项目依赖:
"dependencies": {
"@angular/common": "2.0.0-rc.6",
"@angular/compiler": "2.0.0-rc.6",
"@angular/core": "2.0.0-rc.6",
"@angular/forms": "2.0.0-rc.6",
"@angular/http": "2.0.0-rc.6",
"@angular/platform-browser": "2.0.0-rc.6",
"@angular/platform-browser-dynamic": "2.0.0-rc.6",
"@angular/router": "3.0.0-rc.2",
"ng2-bootstrap": "^1.1.1",
"reflect-metadata": "^0.1.8",
"core-js": "^2.4.0",
"es6-module-loader": "^0.17.8",
"rxjs": "5.0.0-beta.11",
"systemjs": "0.19.27",
"zone.js": "0.6.17",
"jquery": "3.0.0",
}
这是登录模板:
<form #loginForm="ngForm" (ng-submit)="authenticate(loginForm.value)">
</form>
...和登录组件:
import { Component } from '@angular/core';
import {Http, Headers} from '@angular/http';
@Component({
moduleId: module.id,
selector: 'login-cmp',
templateUrl: 'login.component.html'
})
export class LoginComponent {
constructor($http: Http) {
this.$http = $http;
}
authenticate(data) {
...
}
}
我有这个错误:
zone.js?1474211973422:484 Unhandled Promise rejection: Template parse errors:
There is no directive with "exportAs" set to "ngForm" ("
<form [ERROR ->]#loginForm="ngForm"
(ngsubmit)="authenticate(loginForm.value)">
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [FormsModule],
...
})
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
FormsModule //<----------make sure you have added this.
],
....
})
检查是否导入了FormsModule。新表单 angular 2 发行版中没有 ngControl。您必须将模板更改为示例
<div class="row">
<div class="form-group col-sm-7 col-md-5">
<label for="name">Name</label>
<input type="text" class="form-control" required
[(ngModel)]="user.name"
name="name" #name="ngModel">
<div [hidden]="name.valid || name.pristine" class="alert alert-danger">
Name is required
</div>
</div>
</div>
您不仅必须将 FormsModule
导入到根 AppModule 中,而且还必须导入到使用任何 angular 表单指令的每个子模块中。
// SubModule A
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
CommonModule,
FormsModule //<----------make sure you have added this.
],
....
})
(以防别人跟我一样瞎)
form
FTW!确保使用 <form>
标签
不会工作:
<div (ngSubmit)="search()" #f="ngForm" class="input-group">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Go!</button>
</span>
<input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search...">
</div>
魅力十足:
<form (ngSubmit)="search()" #f="ngForm" class="input-group">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Go!</button>
</span>
<input type="text" ngModel class="form-control" name="search" placeholder="Search..." aria-label="Search...">
</form>
你必须关心的三件事:
如果您使用的是子模块,则必须在该子模块中导入
FormModule
:imports:[CommonModule, HttpModule, FormsModule]
您必须导出模块中的
FormModule
:exports:[FormsModule]
所以放在一起看起来像:
imports:[CommonModule, HttpModule, FormsModule], exports:[FormsModule],
在文件的顶部你必须导入
FormsModule
:import {FormsModule} from '@angular/forms';
如果您只使用 app.module.ts
则无需导出。只需要导入。
我在 imports[]
数组中添加了 ReactiveFormsModule
来解决这个错误。
错误:没有将“exportAs”设置为“ngForm”的指令(“
修复:
module.ts:
import {FormsModule, ReactiveFormsModule} from '@angular/forms'
imports: [
BrowserModule,
FormsModule ,
ReactiveFormsModule
],
登录组件ts文件中除了导入form模块外还需要导入NgForm
import { NgForm } from '@angular/forms';
这解决了我的问题
如果您已经导入 FormsModule
,那么您只需从输入字段中删除 formControlName='whatever'
。
你应该用 ctrl+c 终止应用程序并用 ng serve 重新 运行 它,如果你没有将 FormsModule 包含到你 app.module 文件导入数组,然后稍后添加它,angular 不知道,它没有 re-scan 模块,你应该重新启动应用程序,这样 angular 可以看到包含了新模块,之后它将包含模板驱动方法的所有功能
import { FormsModule,ReactiveFormsModule }from'@angular/forms';
imports:[
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
FormsModule,
ReactiveFormsModule/*This one in particular*/,...
],
in app.module.ts 以永久解决 "cannot bind [formGroup] or no directive with export as"
.
如果这样分配名称:
#editForm="testForm"
...必须在组件装饰器中定义 exportAs:
selector: 'test-form',
templateUrl: './test-form.component.html',
styleUrls: ['./test-form.component.scss'],
exportAs: 'testForm'
在我的例子中,我不得不从我的 <form>
标签中删除 ngNoForm
属性。
If you you want to import FormsModule in your application but want to skip a specific form, you can use the ngNoForm directive which will prevent ngForm from being added to the form
参考:https://www.techiediaries.com/angular-ngform-ngnoform-template-reference-variable/
这个
<div #myForm="ngForm"></div>
也会导致错误,可以通过包含 ngForm directive.
来修复<div ngForm #myForm="ngForm"></div>
我刚刚移动了路由模块,即 ARoutingModule 在 FormsModule 和 ReactiveFormsModule 之上,在 CommonModule 之后,在导入的模块数组中。
出于同样的原因,我一遍又一遍地问过同样的问题。因此,让我通过说出我做错了什么来回答这个问题。可能对某人有帮助。
我正在通过 angular-cli
命令
ng g c components/something/something
它所做的,是创建我想要的组件。
此外,在创建组件时,它在应用程序模块的声明数组中添加了组件。
如果是这种情况,请将其删除。 瞧!它可能会起作用。
如果您尝试使用 jasmine 在 angular 中编写单元测试用例,也会发生此错误。
这个错误的基本概念是import FormsModule
。因此,在单元测试文件中,我们添加 imports Section 并将 FormsModule 放在该文件中
TestBed.configureTestingModule
For eg:
TestBed.configureTestingModule({
declarations: [ XYZComponent ],
**imports: [FormsModule]**,
}).compileComponents();
在我的例子中,我忘记将我的组件添加到 app.module.ts 的声明数组中,瞧!问题已解决。
检查另一项:
确保您的组件已添加到 app.module.ts
中 @NgModule 的声明数组@NgModule({
declarations: [
YourComponent,
],
当运行 ng generate component
命令时,它不会自动将其添加到app.module。
是的,我遇到了同样的问题...我做了最重要的事情,但没有成功。 这个解决了我的问题。因为我们在严格模式下使用 angular 'with'
在app.module.ts中添加这段代码
import {FormsModule, ReactiveFormsModule} from '@angular/forms'
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
],
以及您使用的是哪个文件 NgForm 在 .html 文件中
<form #form="ngForm" (ngSubmit)="loginSubmit(form);">
</form>
并在 .ts 文件中
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-login-page',
templateUrl: './login-page.component.html',
styleUrls: ['./login-page.component.scss']
})
export class LoginPageComponent implements OnInit {
NgForm=NgForm;//this one solve my problem...initialization and declaration
}
您必须声明 FormsModule 并且还必须声明 Component
declarations: [
YourComponent -->
],
imports: [
BrowserModule,
FormsModule, -->
],
同意@micronyks 提供的解决方案,但如果您的应用程序很简单且其中没有很多模块,则该解决方案适用。否则,如果您不想将它暴露给所有模块或整个应用程序,我们需要以类似的方式添加到我们使用
几乎所有的答案都在谈论添加 FormsModule
模块,但在我的例子中,导入已经完成。正如在对问题的回答中所说,您必须关闭并重新启动 VScode 或者在我的情况下,您必须使用“将工作区另存为...”来保存您的工作区,然后一切都会再次正常工作。
.HTML file <form (ngSubmit)="addIngredients(f)" #f="ngForm">
.ts file @ViewChild('f', { static: false }) slForm: NgForm;
在 HTML 中将 ngForm 更新为对我有用的 NgForm