我已经将已弃用的表单api的升级推迟到@ angular / forms中的新表单api。我刚刚升级到RC6,现在也想开始使用@ angular / forms。
此时文档非常稀缺,而且我很快陷入困境。
这就是我的工作表格:
<form [ngFormModel]="registrationForm" (ngSubmit)="register(registrationForm.value)">
<fieldset class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="email" [ngFormControl]="registrationForm.controls['email']" maxlength="128" required>
</fieldset>
<div class="alert alert-warning" role="alert" *ngIf="registrationResponse && registrationResponse.EmailExists">
This email account is already linked to an account! <a [routerLink]="['/user/logon']">Go here to login</a>.
</div>
<fieldset class="form-group">
<label for="username">User name</label>
<input type="text" class="form-control" id="username" placeholder="user name" [ngFormControl]="registrationForm.controls['username']" maxlength="32" required>
</fieldset>
<div class="alert alert-warning" role="alert" *ngIf="registrationResponse && registrationResponse.UsernameExists">
This username is already taken! Please choose another one.
</div>
<div>
<button type="submit" class="btn btn-primary" [disabled]="!registrationForm.valid">register</button>
</div>
</form>
在与此模板关联的组件上,FORM_DIRECTIVES在&#34;指令&#34;中指定。组件的属性,它提供了ngFormControl指令等...
如果我理解正确,Control和ControlGroup被重命名为FormControl和FormGroup,并且命令名称也被重命名(ngFormControl等)。所以我更新了我的表格。我用formGroup替换了ngFormModel,并将其替换为:
[ngFormControl]="registrationForm.controls['email']"
通过
formControlName="email"
导致此形式:
<form [formGroup]="registrationForm" (ngSubmit)="register(registrationForm.value)">
<fieldset class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="email" formControlName="email" maxlength="128" required>
</fieldset>
<div class="alert alert-warning" role="alert" *ngIf="registrationResponse && registrationResponse.EmailExists">
This email account is already linked to an account! <a [routerLink]="['/user/logon']">Go here to login</a>.
</div>
<fieldset class="form-group">
<label for="username">User name</label>
<input type="text" class="form-control" id="username" placeholder="user name" formControlName="username" maxlength="32" required>
</fieldset>
<div class="alert alert-warning" role="alert" *ngIf="registrationResponse && registrationResponse.UsernameExists">
This username is already taken! Please choose another one.
</div>
<div>
<button type="submit" class="btn btn-primary" [disabled]="!registrationForm.valid">register</button>
</div>
</form>
但这给了我这个错误:
无法绑定到&#39; formGroup&#39;因为它不是“形式”的已知属性。
我查看了角度代码,我在一些评论中找到了一个提示导入REACTIVE_FORM_DIRECTIVES的示例。但是我无法导入这个(在RC6中是重命名还是删除?),我希望我已经有了这个,因为我的app模块导入了FormsModule(这不是导致整个模块引入的原因吗?):< / p>
@NgModule({
imports: [BrowserModule, FormsModule, HttpModule, appRouterRoot],
providers: [ApiService, LocalStorageService, AuthenticationService, UserService, ForumService],
declarations: [
RootComponent,
RegistrationComponent,
[omitted some components for brevity ....]
],
bootstrap: [RootComponent],
})
export class AppModule {}
有没有人知道如何继续?
编辑:所以,问题已得到解答。但对于其他进行相同升级的人来说:
formControlname的值只是控件的名称,您不需要再指定组名称,这是由formGroupName属性处理的。
答案 0 :(得分:10)
REACTIVE_FORM_DIRECTIVES
。您需要导入FormsModule
和ReactiveFormsModule
:
@NgModule({
import: [
...,
FormsModule,
ReactiveFormsModule
]
})
答案 1 :(得分:3)
还要在代码中导入ReactiveFormsModule。问题如你所说:RC6中不推荐使用REACTIVE_FORM_DIRECTIVES。
答案 2 :(得分:1)
您可以在此处使用新的Forms API找到一堆表单示例: https://github.com/Farata/angular2typescript/tree/master/chapter7/form-samples