I spent probably 2.5 hours on this issue... I was almost banging my head against the wall! :D
Checked another angular component on the project at hand where the dropdown was working correctly and indeed the difference was the [options]
property that was being assigned directly from a var within the component itself. On this one having the issue the [options]
were being dynamically created with a function and I was facing that double click issue. This was breaking the (onChange)
event which was not being fired and hence ruined the expected functionality.
So I got here in this question I think from this issue on Prime-NG GitHub repo. This fortunately made me understand what was the issue.
Searched a bit more using different terms and found this answer here on SO and just adapted it to be used with p-dropdown
like this:
UI side:
<p-dropdown [options]="dynamicOptions"
appendTo="body"
optionLabel="label"
(onChange)="optionChanged(report)"
(onShow)="getDynamicOptions(report)">
The trick here is to use the (onShow)
event to build the dynamic options.
Component side:
export class MyComponent {
dynamicOptions: OptionItem[];
getDynamicOptions(report: Report) {
//debugger;
let dynamicOptions: OptionItem[] = [
{
label: 'No Action',
value: 'no-action'
}
];
// your logic here to build dynamic options...
this.dynamicOptions = dynamicOptions;
}
}