3

I want to wrap a primeng autocomplete component in my own component but can't figure out haw to provide a formControlName:

Error: Uncaught (in promise): Error: formControlName must be used with a parent formGroup directive.

Wrapper component html:

<p-autoComplete
  [formControlName]="formControlName"
  [suggestions]="suggestions"
  [multiple]="multiple"
  [dropdown]="dropdown"
  (completeMethod)="search($event)">
</p-autoComplete>

Wrapper component ts:

@Component({
  selector: 'logi-autocomplete',
  templateUrl: 'autocomplete.component.html',
  providers: [AUTOCOMPLETE_VALUE_ACCESSOR]
})
export class AutocompleteComponent implements OnInit, ControlValueAccessor {

  @Input() formControlName: string;

  @Input() multiple = true;
  @Input() dropdown = true;

  // Skipped non related code

  _value: any = '';
  get value(): any { return this._value; };
  set value(v: any) {
    if (v !== this._value) {
      this._value = v;
      this.onChange(v);
    }
  }

  writeValue(value: any) {
    this._value = value;
    this.onChange(value);
  }

  onChange = (_) => {};
  onTouched = () => {};
  registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
  registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}

Use of the component in a custom for component:

<form [formGroup]="form">
    <logi-autocomplete
      [formControlName]="'groups'"
    ></logi-autocomplete>
</form>
Abbraxar
  • 31
  • 4

1 Answers1

0

You have to write code in the HTML where you want to embed autocomplete.

`

<span class="ui-fluid">
<p-autoComplete formControlName="autoComplete"field="label"completeMethod)="search($event)">
</p-autoComplete>
</span>
 <ul>
     <li *ngFor="let s of autoComplete">{{s.label}}
    </li>
 </ul>

`

and write code in your component where you will pass the data to autocomplete widget.

autoComplete       = new FormControl(null, []);search(event){this.receiptDiarizationFacade.getSubjects(event.query).subscribe(value=>this.autoComplete=value);
}

In this code snapshot, I used observable instead of promise but you can use promise

Parvesh kumar
  • 285
  • 1
  • 3
  • 8