我正在使用带有angular5的prime-ng的p-dropdown。当我打开一个下拉列表包含项目列表的屏幕时,我想从ts中自动选择下拉列表的值。
请注意:它在其他屏幕上均有效,仅在该特定屏幕上不起作用。
下面是我的component.html
<div class="ui-g-6">
<p-dropdown #company="ngModel" [options]="companies" [autoWidth]="false"
[style]="{'width':'100%'}" placeholder="Select a Company" filter="true"
[(ngModel)]="selectedCompany" (onChange)="setBranches()"
name="company" [autoDisplayFirst]="false" required></p-dropdown>
</div>
下面是ts文件的代码,
this.companies = [];
if (this.customerGroups) {
this.customerGroups.customers.forEach((customer) => {
this.companies.push({
label: customer.company,
value: customer
});
});
} else {
this.companies.push({
label: this.campaign.customer.company,
value: this.campaign.customer
});
}
this.companies = JSON.parse(JSON.stringify(this.companies));
this.setSelectedCompany();
setSelectedCompany() {
this.companies.forEach((company, index) => {
if (this.invoice.id === null || this.invoice.id === undefined) {
if (company.label === this.campaign.customer.company) {
// this.selectedCompany = company.value;
this.selectedCompany = this.companies[index].value;
}
} else {
if (company.label === this.invoice.campaign.customer.company) {
// this.selectedCompany = company.value;
this.selectedCompany = this.companies[index].value;
}
}
});
我认为这可能与屏幕渲染有关,任何人都可以帮忙。