首页 > 解决方案 > Primeng 下拉菜单不显示值

问题描述

我面临一个奇怪的问题。我想从 http 请求中填充下拉列表。声明为 Agence 对象的数组填充了来自我的后端的正确属性,但它无法显示值。

<p-dropdown [options]="agences" [(ngModel)]="codeAgence" optionLabel="code" [ngModelOptions]=" 
{standalone: true}"></p-dropdown>

我在数组上手动推送第一行,下拉列表仅显示添加的这一行,但忽略来自 http 请求的其他属性。奇怪的是,该数组在 http 请求之后确实具有正确的属性,如此屏幕截图所示

代理数组

我的组件的代码在这里:

interface Agence {
    name: string,
    code: string
}

export class Agences {
  id: string;
  label: string;
  order: string;
  code: string;

  fonctions: Fonction[];

  constructor(values: Object = {}) {
       Object.assign(this, values);
  }
}

@Component({
  selector: 'app-gestion-courrier',
  templateUrl: './gestion-courrier.component.html',
  styleUrls: ['./gestion-courrier.component.css']
})
export class GestionCourrierComponent implements OnInit {

  agences:Agence[] = [];
  listeAgences: Agences[] = [];

ngOnInit() {
  this.getAllAgences();
}

getAllAgences() {
  this.http.get(endPointAgences).subscribe((result: Agences[]) => {
  this.listeAgences = result;
  for (let i in this.listeAgences) {
    this.agences.push(this.listeAgences[i].label, this.listeAgences[i].code);
  }
})
}

标签: arraysangulartypescriptprimengprimeng-dropdowns

解决方案


推荐阅读