2

I receive the value of myModel.field correctly from a server (for example myModel.field = "id2"), but it is not selected in the p-dropdown, what should I do?

This is my options (the dropdown populates fine):

options = [
    { label: 'Opt1', value: 'id1' },
    { label: 'Opt2', value: 'id2' },
    { label: 'Opt3', value: 'id3' },
    { label: 'Opt4', value: 'id4' },
   ]

This is my dropdown in .html

<p-dropdown 
    [options]="options" [(ngModel)]="myModel.field" 
    optionLabel="label" optionValue="value" 
    [showClear]="true" [filter]="true" filterBy="label" 
    required="true">
</p-dropdown>
doinel
  • 87
  • 1
  • 9
  • 1
    What version are you using? I am trying with version 11 and it works fine. Maybe, if you can do a stackblitz or something like that, it would be fantastic. – Alba Feb 08 '21 at 15:47
  • 1
    That should be working fine (although you shouldn't need to define `optionLabel` and `optionValue` when they have the default names in the object. Please check two different options (default object and custom object) in the following stackblitz https://stackblitz.com/edit/angular-ivy-ef94vu – Hugo Noro Feb 08 '21 at 16:10
  • 1
    Could have something to do with change detection. When you are setting a field of myModel object, you are not changing the reference of the object. In some implementations, depending on the strategy (for example OnPush), components are not updated unless the reference changes or you manually detect changes.. To give a complete, sound answer, please provide an example on stackblitz for example.. – Cédric S Feb 08 '21 at 16:14
  • Thanks everyone for the advice. I used the stackblitz of Hugo Noro to replicate my problem and realized that in my case "value" loaded into options was a number, while the value passed by the server was a string, so they looked the same, but they weren't. Sorry if I have wasted your time for such a banality – doinel Feb 09 '21 at 15:29

0 Answers0