components icon indicating copy to clipboard operation
components copied to clipboard

mdAutocomplete Can't bind to 'formControl'

Open kstraszewski opened this issue 9 years ago • 18 comments

Bug, feature request, or proposal:

Bug

What is the expected behavior?

I wanted to try out the mdAutocomplete component.

What is the current behavior?

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

What are the steps to reproduce?

I was trying this example from "Examples tab": https://material.angular.io/components/component/autocomplete I just do simple copy and paste to my html and ts file.

Which versions of Angular, Material, OS, browsers are affected?

I'm using master tree from: https://github.com/angular/material2-builds.git

kstraszewski avatar Mar 18 '17 14:03 kstraszewski

Have you added ReactiveForm in imports ?

diicar avatar Mar 18 '17 15:03 diicar

Ok i haven't added ReactiveForm to imports, but anyway i did it [(ngModel)] way.

kstraszewski avatar Mar 18 '17 15:03 kstraszewski

Yep, this looks like you were just missing the module import. Closing.

kara avatar Mar 18 '17 21:03 kara

I am facing same problem.. what is the solution ?

param-bnb avatar Apr 04 '17 07:04 param-bnb

@param-bnb Probably the import of ReactiveForm as mentioned before.

dahaupt avatar Apr 04 '17 13:04 dahaupt

I am also faced with the same issue even after importing ReactiveFormsModule. Any suggestions?

ATakaSKY avatar Apr 10 '17 13:04 ATakaSKY

formControl is a directive from @angular/forms, so it's unlikely Material has anything to do with that error. Are you certain you have the ReactiveFormsModule loaded for that component?

kara avatar Apr 10 '17 18:04 kara

I too am having the same issue. Even after importing Reactive forms. But the error changed slightly, now it reads: Error: No provider for FormControl! --housing-address.comonent.ts

...
import { FormControl } from '@angular/forms';
...
export class HousingAddressFormComponent implements OnInit {
...
stateCtrl: FormControl;
  filteredStates: any;
...
constructor (
   ...
    ) {
  this.stateCtrl = new FormControl();
  this.filteredStates = this.stateCtrl.valueChanges
      .startWith(null)
      .map(name => this.filterStates(name));
}
...
  filterStates(val: string) {
    return val ? this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s))
      : this.states;
  }

---housing-address.component.html

...
<md-input-container>
          <input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
        </md-input-container>

        <md-autocomplete #auto="mdAutocomplete">
          <md-option *ngFor="let state of filteredStates | async" [value]="state.value">
            {{ state.key }}
          </md-option>
        </md-autocomplete>
...

daddyschmack avatar Apr 12 '17 21:04 daddyschmack

@daddyschmack Can you reproduce in a plunker?

kara avatar Apr 12 '17 21:04 kara

Hi kara, I found the issue, I had double declarations of the FormControl...

daddyschmack avatar Apr 12 '17 22:04 daddyschmack

Step 3: Import the component modules

Import the NgModule for each component you want to use:

import {MdButtonModule, MdCheckboxModule} from '@angular/material';

@NgModule({ ... imports: [MdButtonModule, MdCheckboxModule], ... }) export class PizzaPartyAppModule { } Alternatively, you can create a separate NgModule that imports all of the Angular Material components that you will use in your application. You can then include this module wherever you'd like to use the components.

import {MdButtonModule, MdCheckboxModule} from '@angular/material';

@NgModule({ imports: [MdButtonModule, MdCheckboxModule], exports: [MdButtonModule, MdCheckboxModule], }) export class MyOwnCustomMaterialModule { } Whichever approach you use, be sure to import the Angular Material modules after Angular's BrowserModule, as the import order matters for NgModules.

elbembo avatar May 16 '17 14:05 elbembo

That you need ReactiveFormsModule is neither mentioned in https://angular.io/guide/forms nor in https://material.angular.io/components/autocomplete/overview or https://material.angular.io/guide/getting-started 😞

you are just facing

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

and asking google for help. :P

MA-Maddin avatar Aug 10 '17 17:08 MA-Maddin

I would like to add that I ran into this bug because of casing - I had used [FormControl] instead of [formControl] - this is why adding the ReactiveFormsModule didn't help :)

iJungleboy avatar Sep 12 '17 20:09 iJungleboy

Big thanks for ReactiveFormsModule hints - that helped a lot.

I second @MA-Maddin 's comment - this requirement should be highlighted. Everything only seems to mention FormsModule.

Eventually digging around the example code shows it in the autogenerated example-modules.ts: import {FormsModule, ReactiveFormsModule} from '@angular/forms';

Simple fix to the docs?

StephM-W avatar Sep 29 '17 10:09 StephM-W

import {FormsModule, ReactiveFormsModule} from '@angular/forms'; does not fix in my issue

dapojaja avatar Dec 19 '17 13:12 dapojaja

@dapojaja you need to add ReactiveFormsModule in imports array in @NgModule.

Rajasree37 avatar Feb 14 '18 09:02 Rajasree37

I think, i have the same probleme, someone can help me please ?https://stackoverflow.com/questions/50928851/mat-autocomplete-in-formgroup-customfilter

alanent avatar Jun 19 '18 16:06 alanent

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.