Angular 2个下拉列表默认值

Angular 2 dropdownlist default values

我有一个从数据库中获取值的下拉列表,我想要一个像 "select a item" 这样的默认值。我该怎么做?

HTML代码

<div class="ui-grid-row">
    <div class="ui-grid-col-2">
        Period :
    </div>
    <div class="ui-grid-col-4">
        <p-dropdown [options]="periodids" formControlName="periodid" [(ngModel)]="periodid" [autoWidth]="false" [style]="{'text-align': 'left'}"></p-dropdown>
    </div>
</div>

从数据库填充下拉列表

populatePeriod() {
    this.periodids = [];
    this.periodservice.getPeriods().then(returneddata => {
        for (let i = 0; i < returneddata.length; i++) {
            this.periodids.push({ label: returneddata[i].PeriodName, value: returneddata[i].Id });
        }
    });
    //console.log(this.periodids)
}

PrimeNG documentation 中的示例建议您将其添加到您的选项数组中:

populatePeriod() {
    this.periodids = [{ label: "Select an item...", value: null }]; // <-- here

    this.periodservice.getPeriods().then(returneddata => {
        for (let i = 0; i < returneddata.length; i++) {
            this.periodids.push({ label: returneddata[i].PeriodName, value: returneddata[i].Id });
        }
    });

    console.log(this.periodids)
}