[英]How to remove pre-selection in PrimeNg dropdown
I'm using p-dropdown
from PrimeNg 15
.我正在使用
PrimeNg 15
p-dropdown
。 It's a very simple dropdown.这是一个非常简单的下拉菜单。
HTML: HTML:
<form [formGroup]="myReactiveForm">
...
<b>Dropdown:</b>
<p-dropdown
[options]="colors"
optionLabel="name"
formControlName="color" // <-----------Form control variable
></p-dropdown>
...
</form>
TS: TS:
colors = [
{ name: 'black', code: 'blk' },
...
];
constructor() {
this.myReactiveForm = new FormGroup({
...
color: new FormControl(), // <----- kept this contructor blank
...
});
}
ngOnInit() {
this.clearForm();
}
clearForm() {
this.myReactiveForm.controls.color.setValue(null);
}
Still I can see the first item ie 'black' is selected in the dropdown.我仍然可以看到在下拉列表中选择了第一项,即“黑色”。 Please pitch in. Here is the Stackblitz .
请参与进来。这里是Stackblitz 。
You need to bind [autoDisplayFirst]
to the p-dropdown
您需要将
[autoDisplayFirst]
绑定到p-dropdown
<p-dropdown
[options]="colors"
optionLabel="name"
[autoDisplayFirst]="false"
formControlName="color"
></p-dropdown>
The docs says about the option:该文档说明了该选项:
Whether to display the first item as the label if no placeholder is defined and value is null.
如果未定义占位符且值为 null,是否将第一项显示为 label。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.