Search code examples
angularangular-materialangular-forms

Angular form element in separated component gives FormGroup error


I have a form element made with Angular Material which is a autocompleted country selectbox:

<mat-form-field>
  <input type="text" placeholder="Country" aria-label="Number" matInput formControlName="country" [matAutocomplete]="auto">
  <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
    <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
      {{ option.name }}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

In the ngOnInit I call an API and this populates the filteredOptions.

Since I am going to use this picker in many forms I want to make a component of it. As soon as I move the code to a component I got:

SelectCountryComponent.html:2 ERROR Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass it an existing FormGroup instance (you can create one in your class).

Even though I use my component in a FormGroup it still gives this error. What would be the right approach to create a component which contains a formControl?

select-country.component.ts

@Component({
  selector: 'app-select-country',
  templateUrl: './select-country.component.html',
  styleUrls: ['./select-country.component.css']
})
export class SelectCountryComponent implements OnInit {

  countryFormGroup = new FormGroup({
    country: new FormControl('')
  });

  options: Country[];
  filteredOptions: Observable<Country[]>;

  constructor(public api: ApiService) {

  }

  ngOnInit() {

    this.api.get('country').subscribe(res => {
      console.log(res);
      this.options = res;
      this.filteredOptions = this.countryFormGroup.get('country').valueChanges.pipe(
        startWith<string | Country>(''),
        map(value => (typeof value === 'string' ? value : value['name'])),
        map(name => name ? this._filter(name) : this.options ? this.options.slice() : [])
      );
    });

  }

  displayFn(country?: Country): string | undefined {
    return country ? country.name : undefined;
  }

  private _filter(name: string): Country[] {
    const filterValue = name.toLowerCase();

    return this.options.filter(
      option => option.name.toLowerCase().indexOf(filterValue) === 0
    );
  }

}

select-country.component.html

<mat-form-field>
  <input type="text" placeholder="Country" aria-label="Number" matInput formControlName="country" [matAutocomplete]="auto">
  <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
    <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
      {{ option.name }}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

Solution

  • In your case:

    <form [formGroup]="countryFormGroup" (ngSubmit)="onSubmit()">
      <mat-form-field>
        <input type="text" placeholder="Country" aria-label="Number" matInput formControlName="country" [matAutocomplete]="auto">
      </mat-form-field>
    </form>
    

    You need to bind countryFormGroup with form by [formGroup]="countryFormGroup" and use formControlName inside it.

    To make your code cleaner, you can filter options using pipe in HTML