可以使用以下示例代码解决该问题:
在组件中引入下拉框组件:
import { SelectItem } from 'primeng/components/common/selectitem';
import {DropdownModule} from 'primeng/dropdown';
创建下拉框数据源:
cars: SelectItem[];
selectedCar: string;
初始化下拉框数据:
this.cars = [];
this.cars.push({label:'Audi', value:'Audi'});
this.cars.push({label:'BMW', value:'BMW'});
this.cars.push({label:'Fiat', value:'Fiat'});
this.cars.push({label:'Ford', value:'Ford'});
this.cars.push({label:'Honda', value:'Honda'});
this.cars.push({label:'Jaguar', value:'Jaguar'});
this.cars.push({label:'Mercedes', value:'Mercedes'});
this.cars.push({label:'Renault', value:'Renault'});
this.cars.push({label:'VW', value:'VW'});
在HTML中使用下拉框组件:
其中,[options]属性指定数据源,[(ngModel)]属性双向绑定选择的值。