It is impossible to associate with 'formControl', since this is not a known property of the "input" - angular2 material. Autofill

I am trying to use angular autocomplete stuff in my angular2 project. I have added the following to my template.

<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> 

Below is my component.

 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; } } 

I get the following error. It looks like the formControl directive formControl not found.

Cannot bind to 'formControl' because it is not a known property of 'input'

What is the problem?

+185
angular typescript angular-material2
Apr 05 '17 at 1:32 on
source share
2 answers

When using formControl you must import the ReactiveFormsModule into your imports array.

Example:

 import {FormsModule, ReactiveFormsModule} from '@angular/forms'; @NgModule({ imports: [ BrowserModule, FormsModule, ReactiveFormsModule, MaterialModule, ], ... }) export class AppModule {} 
+408
Apr 05 '17 at 2:32
source share

Forget trying to decrypt the .ts example - as others have said, it is often incomplete.

enter image description here

Instead, simply click on the pop-up icon circled here and you will get a fully working example .

You can quickly confirm the necessary modules:

enter image description here

Comment out all instances of ReactiveFormsModule and make sure you get the error:

 Template parse errors: Can't bind to 'formControl' since it isn't a known property of 'input'. 
+15
Jan 01 '18 at 2:48
source share



All Articles