gpt4 book ai didi

angular - Angular 2 RC4 中的表单

转载 作者:太空狗 更新时间:2023-10-29 17:02:37 24 4
gpt4 key购买 nike

我正在 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>

这里有人知道警告所指的新表单模块是什么吗?哪些指令会发生变化?

最佳答案

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

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

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

虽然 FormBuilder 没有被弃用,但您可以直接使用 FormGroup 类:

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

关于angular - Angular 2 RC4 中的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38197255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com