Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind to 'formControl' since it isn't a known property of 'input' - Angular2 Material Autocomplete issue

I am trying to use Angular Material Autocomplete component in my Angular 2 project. I 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> 

Following 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'm getting the following error. It looks like the formControl directive is not being found.

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

What is the issue here?

like image 380
Lahiru Chandima Avatar asked Apr 05 '17 01:04

Lahiru Chandima


People also ask

Can't bind to form since it isn't a known property of input?

In order to solve can't bind to 'formgroup' since it isn't a known property of 'form' error you need to import ReactiveFormsModule in each submodule file.

Can't bind to exclude since it isn't a known property of A?

What does "Can't bind to 'x' since it isn't a known property of 'y'" mean? link. This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application sub-module but forgot to export it.

Can't bind to FormGroup since it isn't a known property of?

What Does This Error Mean? Angular is trying to tell us that it doesn't know about the formGroup directive on the <form> element in your component. This usually happens when the wrong forms module is imported, the right module is imported in the wrong place or the ReactiveFormsModule is just not imported at all.

What is ReactiveFormsModule in Angular?

A directive which installs the MinValidator for any formControlName , formControl , or control with ngModel that also has a min attribute. NgControlStatus. Directive automatically applied to Angular form controls that sets CSS classes based on control status. NgControlStatusGroup.


2 Answers

While using formControl, you have to import ReactiveFormsModule to your imports array.

Example:

import {FormsModule, ReactiveFormsModule} from '@angular/forms';  @NgModule({   imports: [     BrowserModule,     FormsModule,     ReactiveFormsModule,     MaterialModule,   ],   ... }) export class AppModule {} 
like image 62
Pengyy Avatar answered Oct 11 '22 15:10

Pengyy


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

Instead just click on the 'pop-out' icon circled here and you'll get a fully working StackBlitz example.

enter image description here

You can quickly confirm the required modules:

enter image description here

Comment out any instances of ReactiveFormsModule, and sure enough you'll get the error:

Template parse errors: Can't bind to 'formControl' since it isn't a known property of 'input'.  
like image 36
Simon_Weaver Avatar answered Oct 11 '22 15:10

Simon_Weaver