i'm on the RC4 and i'm getting the error There is no directive with "exportAs" set to "ngForm" because of my template :
<div class="form-group">
<label for="actionType">Action Type</label>
<select
ngControl="actionType"
===> #actionType="ngForm"
id="actionType"
class="form-control"
required>
<option value=""></option>
<option *ngFor="let actionType of actionTypes" value="{{ actionType.label }}">
{{ actionType.label }}
</option>
</select>
</div>
the boot.ts :
import {disableDeprecatedForms, provideForms} from '@angular/forms';
import {bootstrap} from '@angular/platform-browser-dynamic';
import {HTTP_PROVIDERS, Http} from '@angular/http';
import {provideRouter} from '@angular/router';
import {APP_ROUTER_PROVIDER} from './routes';
import {AppComponent} from './app.component';
bootstrap(AppComponent, [ disableDeprecatedForms(), provideForms(), APP_ROUTER_PROVIDER, HTTP_PROVIDERS]);
/// so here is my DropdownList :
<fieldset ngControlGroup="linkedProcess" >
<div ngControlGroup="Process" >
<label>Linked Process</label>
<div class="form-group">
<select
ngModel
name="label"
#label="ngModel"
id="label"
class="form-control" required
(change)="reloadProcesse(list.value)"
#list>
<option value=""></option>
<!--<option value=`{{ActionFormComponent.getFromString('GET'')}}`></option>-->
<option *ngFor="let processus of linkedProcess?.processList?.list; let i = index"
value="{{ processus[i].Process.label}}">
{{processus.Process.label}}
</option>
</select>
</div>
</div>
//my component ts :
i was representing it in the old forms like this :
categoryControlGroups:ControlGroup[] = [];
categories:ControlArray = new ControlArray(this.categoryControlGroups);
and now i'm doing this :
categoryControlGroups:FormGroup[] = [];
categories:FormArray = new FormArray(this.categoryControlGroups);
you think it's the cause of the prob ??
In my case I had to add
FormsModule
andReactiveFormsModule
to theshared.module.ts
too:(thanks to @Undrium for the code example):
I had this problem and I realized I had not bound my component to a variable.
Changed
<input #myComponent="ngModel" />
to
<input #myComponent="ngModel" [(ngModel)]="myvar" />
Since 2.0.0.rc6:
In short:
FormsModule
to your@NgModule
.ReactiveFormsModule
to your@NgModule
.So, add to your
app.module.ts
or equivalent:Failing to have one of these modules can lead to errors, including the one you face:
If you're in doubt, you can provide both the
FormsModule
and theReactiveFormsModule
together, but they are fully-functional separately. When you provide one of these modules, the default forms directives and providers from that module will be available to you app-wide.Old Forms using
ngControl
?If you do have those modules at your
@NgModule
, perhaps you are using old directives, such asngControl
, which is a problem, because there's nongControl
in the new forms. It was replaced more or less* byngModel
.For instance, the equivalent to
<input ngControl="actionType">
is<input ngModel name="actionType">
, so change that in your template.Similarly, the export in controls is not
ngForm
anymore, it is nowngModel
. So, in your case, replace#actionType="ngForm"
with#actionType="ngModel"
.So the resulting template should be (
===>
s where changed):* More or less because not all functionality of
ngControl
was moved tongModel
. Some just were removed or are different now. An example is thename
attribute, the very case you are having right now.I had this problem because I had a typo in my template near [(ngModel)]]. Extra bracket. Example:
The correct way of use forms now in Angular2 is:
The old way doesn't works anymore
I had the same problem which was resolved by adding the FormsModule to the .spec.ts:
and then adding the import to beforeEach: