我正在尝试在我的Angular 2项目中使用Angular Material Autocomplete组件。我在模板中添加了以下内容。
<md-input-container>
<input mdInput placeholder="Category" [mdAutocomplete]="auto" [formControl]="stateCtrl">
</md-input-container>
<md-autocomplete #auto="mdAutocomplete">
<md-option *ngFor="let state of filteredStates | async" [value]="state">
{{ state }}
</md-option>
</md-autocomplete>
以下是我的组件。
import {Component, OnInit} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {FormControl} from "@angular/forms";
@Component({
templateUrl: './edit_item.component.html',
styleUrls: ['./edit_item.component.scss']
})
export class EditItemComponent implements OnInit {
stateCtrl: FormControl;
states = [....some data....];
constructor(private route: ActivatedRoute, private router: Router) {
this.stateCtrl = new FormControl();
this.filteredStates = this.stateCtrl.valueChanges.startWith(null).map(name => this.filterStates(name));
}
ngOnInit(): void {
}
filterStates(val: string) {
return val ? this.states.filter((s) => new RegExp(val, 'gi').test(s)) : this.states;
}
}
我遇到了以下错误。看起来formControl
指令没有被找到。
无法将'formControl'绑定到'input',因为它不是'input'的已知属性
这里出现了什么问题?
formControl
时,你需要将ReactiveFormsModule
导入到你的模块中,而不是根模块。以防你在功能模块中使用FormControl
。 - King Johnformcontrol
(小写)而不是formControl
— 如果你正在通过 webpack html-loader 运行模板,这将会有所帮助:https://dev59.com/Hpzha4cB1Zd3GeqPGYj0#40626329 - Ben Boyle