• Register
0 votes
2.7k views

Problem :

I am beginner in using Angular material. I am trying to use autocomplete component in the Angular2 project.

Please find below the code written for my component.

import {Component, OnInit} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {FormControl} from "@angular/forms";

@Component({
    mytemplateUrl: './edit_item.component.html',
    mystyleUrls: ['./edit_item.component.scss']
})
export class MyStateComponent implements OnInit {
    mystateCtrl: FormControl;
    mystates = [....states related data....];

    constructor(private route: ActivatedRoute, private router: Router) {
        this.mystateCtrl = new FormControl();
        this.filteredStates = this.mystateCtrl.valueChanges.startWith(null).map(name => this.filterStates(name));
    }
    ngOnInit(): void {
    }
    filterStates(val: string) {
        return val ? this.mystates.filter((s) => new RegExp(val, 'gi').test(s)) : this.mystates;
    }
}

But I am facing a below error. I guess my formControl directive is not located.

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

I am struggling with this issue from past 2 days but unable to resolve it.

Can somebody help me in fixing this issue?

2.3k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I had also faced this issue related to formControl this is very tricky issue and so beginners often find it very difficult to fix it.

When I was using the formControl I had imported ReactiveFormsModule to my imports array as shown below.

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

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    MaterialModule,
  ],
  ...
})
export class MyStateComponent implements OnInit{}
I hope my solution will save your time and fix your issue.
5k points