在 RC6 中升级到 @angular/forms

Upgrading to @angular/forms in RC6

我一直在推迟从 @angular/forms 中已弃用的表单 api 到新表单 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>

在与此模板关联的组件上,在组件的 "directives" 属性中指定了 FORM_DIRECTIVES,它提供了 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>

但这给了我这个错误:

Can't bind to 'formGroup' since it isn't a known property of 'form'.

我查看了 angular 代码,并且在一些提到导入 REACTIVE_FORM_DIRECTIVES 的评论中找到了示例。但是我无法导入这个(它在 RC6 中重命名或删除了吗?),我希望我已经有了这个,因为我的应用程序模块导入 FormsModule(这不是引入整个模块的原因吗?):

@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 属性处理的。

REACTIVE_FORM_DIRECTIVES 在 RC6 中被弃用并删除。您需要导入 FormsModuleReactiveFormsModule:

@NgModule({
  import: [
    ...,
    FormsModule,
    ReactiveFormsModule
  ]
})

您可以在此处找到大量使用新表单 API 的表单示例: https://github.com/Farata/angular2typescript/tree/master/chapter7/form-samples

也请在您的代码中导入 ReactiveFormsModule。问题如您所说:REACTIVE_FORM_DIRECTIVES 在 RC6 中已弃用。