简体   繁体   中英

How to set defualt value for primeng autocomplete <p-autocomplete>

I tried below but didn't worked.

Added below to <p-autocomplete>

[ngModelOptions]="{standalone: true}" and [(ngModel)]="sampleModelData"

and in .ts file added

this.sampleModelData='ramesh';

even I tried setting as below in .ts file but this too didn't work. (<HTMLInputElement>document.getElementById('sampleModelData')).value='ramesh123';

How to resolve issue

you need to get a reference to the autocomplete component that you can access to the input native element

  @ViewChild('autoItems', { static: true }) public autoItems: AutoComplete; // 👈

  setAutoValue() {

    this.selectedValue = {label:'Apple' , value:'apple'}; 
    this.autoItems.inputEL.nativeElement.value = this.selectedValue.label; // 👈
  }

template

<p-autoComplete [(ngModel)]="selectedValue" field="label" [suggestions]="result" 
   (completeMethod)="search($event)"#autoItems>
</p-autoComplete>

demo 🚀

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM