无法绑定到’formControl’,因为它不是’input’的已知属性 – angular2材质自动完成问题

前端之家收集整理的这篇文章主要介绍了无法绑定到’formControl’,因为它不是’input’的已知属性 – angular2材质自动完成问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在angular2项目中使用角度材料 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指令.

Can’t bind to ‘formControl’ since it isn’t a known property of ‘input’

这是什么问题?

使用formControl时,必须将ReactiveFormsModule导入导入数组.

例:

import {FormsModule,ReactiveFormsModule} from '@angular/forms';

@NgModule({
  imports: [
    BrowserModule,FormsModule,ReactiveFormsModule,MaterialModule,],...
})
export class AppModule {}

猜你在找的Angularjs相关文章