4

我正在使用primeng下拉菜单并且很难将我的对象竞标到下拉菜单。它确实用空项目填充下拉列表..不确定如何指定字段名称。

HTML

<div class="form-group col-xs-3 col-md-3"
                                     [ngClass]="{
                                     'has-error':((ersaForm.get('costCenter').touched || ersaForm.get('costCenter').dirty ) && 
                                      !ersaForm.get('costCenter').valid) || (ersaForm.get('costCenter').value.cost_center_name == '')
                                     }">
 <label for="costCenterId" class="control-label">Cost Center</label>
 <p-dropdown [options]="iCostCenter" styleClass="form-control" formControlName="costCenter" placeholder="Cost Center (required)" id="costCenterId" name="costCenter"  dataKey="cost_center_id">
</p-dropdown>

目的

    {
  "result": [
    {
      "cost_center_id": 0,
      "cost_center_name": "0"
    },
    {
      "cost_center_id": 1,
      "cost_center_name": "1"
    },
    {
      "cost_center_id": 2,
      "cost_center_name": "2"
    },
}

TS

    export interface ICostCenter{

        cost_center_id: string,
        cost_center_name: string
    }

   iCostCenter: ICostCenter[];
    this._appParams.getAllCostCenters()
            .subscribe(
            data => {
                this.iCostCenter = data.result;

            }
4

2 回答 2

5

如果您查看官方文档,它说optionLabel如果您绑定到任意对象的集合,则需要使用属性。

<p-dropdown [options]="iCostCenter" optionLabel="cost_center_name" styleClass="form-control" formControlName="costCenter" placeholder="Cost Center (required)" id="costCenterId" name="costCenter"  dataKey="cost_center_id">
</p-dropdown>
于 2019-02-26T21:55:50.377 回答
2
<p-dropdown [options]="iCostCenter" 
  optionLabel="cost_center_name" 
  styleClass="form-control" 
  formControlName="costCenter" 
  placeholder="Cost Center (required)" 
  id="costCenterId" 
  name="costCenter"
  dataKey="cost_center_id" 
  optionValue="costCenter">
</p-dropdown>

您可以使用optionValue将特定值设置为,formControl而不是Object.dataKey用于唯一标识选项中的值的属性。

于 2021-03-26T05:16:02.037 回答