I keep getting this error while using TypeScript\'s Angular2-forms framework:
There is no
directive
with \"exportAs\" set to \"ngForm
import { FormsModule,ReactiveFormsModule }from'@angular/forms';
imports:[
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
FormsModule,
ReactiveFormsModule/*This one in particular*/,...
],
in app.module.ts to permanently solve errors like "cannot bind [formGroup] or no directive with export as"
.
In case a name is assigned like this:
#editForm="testForm"
... the exportAs has to be defined in the component decorator:
selector: 'test-form',
templateUrl: './test-form.component.html',
styleUrls: ['./test-form.component.scss'],
exportAs: 'testForm'
check whether you import FormsModule. There's no ngControl in the new forms angular 2 release version. you have to change your template to as an example
<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>
I've come to this same question over & over again, also due to same reason. So let me answer this by saying what wrong I was doing. Might be helpful for someone.
I was creating component via angular-cli
by command
ng g c components/something/something
What it did, was created the component as I wanted.
Also, While creating the component, it added the component in the App Module's declarations array.
If this is the case, please remove it. And Voila! It might work.
You have to import FormsModule
into not only the root AppModule, but also into every subModule that uses any angular forms directives.
// SubModule A
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
CommonModule,
FormsModule //<----------make sure you have added this.
],
....
})
This
<div #myForm="ngForm"></div>
Also causes the error and can be fixed by including the ngForm directive.
<div ngForm #myForm="ngForm"></div>