微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

角色2 RC4中的形式

我正在试验角度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>

有谁在这里知道警告所说的新形式模块是什么,哪些指令会改变什么?

您需要在引导应用程序时明确禁用不支持的表单支持
import {disableDeprecatedForms,provideForms} from '@angular/forms';

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

而FormBuilder不被弃用,您可以直接使用FormGroup类:

this.filterForm = new FormGroup({
  title: new FormControl('',Validators.required)
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐