我正在将我的Angular应用程序从rc4升级到rc5,包括将Angular表单从0.2.0升级到0.3.0。
从那以后,我得到了以下错误,这似乎与表单0.3.0中ngformcontrol的更改有关
zone.js:461 Unhandled Promise rejection: Template parse errors:
Can't bind to 'ngFormControl' since it isn't a known property of 'input'. ("nput type="text" class="form-control search-input" name="search" placeholder="Search..." [value]="" [ERROR ->][ngFormControl]="searchFormControl">
该行引用了我的一个组件模板中的搜索框代码:
<p [hidden]=hideSearch>
<input type="text" class="form-control search-input" name="search" placeholder="Search..." [value]="" [ngFormControl]="searchFormControl">
</p>
我找不到任何有关此功能已更改的信息。那么,现在ngformcontrol的正确替代方案是什么?
编辑:对于context,这里是整个component header.component.ts(formmodule和reactiveformmodule加载在app.module.ts中)
import { Component, EventEmitter, Input, Output} from '@angular/core';
import { Control } from '@angular/common';
import { OrbitService } from './orbit.service';
import 'rxjs/add/operator/debounceTime';
@Component({
selector: 'orbit-header',
template: `
<div class="top-nav row">
<h3 class="pull-left">Northmark</h3>
<div class="col-md-2 col-md-offset10 pull-right">
<p [hidden]=hideSearch>
<!-- ng-model = [ngFormController] -->
<input type="search" class="form-control search-input" name="search" placeholder="Search..." [value]="" [NgFormControl]="searchFormControl">
</p>
<p>
<a class="log-out" (click)="orbitService.deleteCookie('ORBIT_COOKIE')">Log Out</a>
</p>
</div>
<div class="clearfix"></div>
</div>
`
})
export class HeaderComponent implements OnInit {
@Input() private hideSearch: boolean;
constructor(
private orbitService: OrbitService) {
this.searchFormControl = new Control();
}
@Output() onSubmit = new EventEmitter<string>();
ngOnInit() {
this.searchFormControl.valueChanges
.debounceTime(500)
.subscribe(query => this.submit(query));
}
submit(query: string) {
this.onSubmit.emit(query);
}
}
最佳答案
[ngFormControl]="searchFormControl"
应该是
[formControl]="searchFormControl"
我认为你需要导入
ReactiveFormsModule