Angular 2 RC4中的表格

时间:2016-07-05 07:08:36

标签: angular

我正在试验Angular 2 RC4中的表格。 一切正常,但是当我启动应用程序时,浏览器控制台会给我这样的信息:

*It looks like you're using the old forms module. This will be opt-in in the next RC, and will eventually be removed in favor of the new forms module.

我的组件的相关部分如下所示:

import {
    FORM_DIRECTIVES,
    REACTIVE_FORM_DIRECTIVES,
    FormBuilder,
    FormGroup
} from '@angular/forms';
import {Observable} from "rxjs/Rx";

@Component
({
    selector: "hh-topbar",
    moduleId: module.id,
    templateUrl: "topBar.component.html",
    directives: [HHPagerComponent, FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES]
})

export class HHTopBarComponent implements OnChanges, OnInit
{
    ...
    private filterForm: FormGroup;
    private title$: Observable<string>;

    constructor(private formBuilder: FormBuilder)
    {
    }

    public ngOnInit(): any
    {
        this.filterForm = this.formBuilder.group
        ({
            "title": [this.info.filters.searchFileName]
        });

        this.title$ = this.filterForm.controls["title"].valueChanges;
        this.title$.subscribe(val =>
        {
            this.info.filters.searchFileName = val;
            this.filterChanged.emit(this.info.filters);
        });
    }
}

我模板的相关部分如下所示:

<form [formGroup]="filterForm">
    <div>
        <label for="title">Title</label>
        <input [formControl]="filterForm.controls['title']" id="title" />
    </div>
</form>

这里有人知道警告所讨论的新表格模块是什么,以及哪些指令会改变以及是什么?

1 个答案:

答案 0 :(得分:16)

在引导应用程序时,您需要显式禁用已弃用的表单支持:

import {disableDeprecatedForms, provideForms} from '@angular/forms';

bootstrap(AppComponent, [
  disableDeprecatedForms()
  provideForms()
]);

虽然{@ 1}}不被弃用,但您可以直接使用FormBuilder类:

FormGroup