繁体   English   中英

Angular ng-select : selectedItems.map 不是一个函数

[英]Angular ng-select : selectedItems.map is not a function

当我以反应形式 angular 使用 ng-select 时,我收到此错误:

错误类型错误:selectedItems.map 不是函数

我有 3 个选择前两个工作得很好,但在第三个中我得到了错误! 映射项目我正在使用该函数(ng-select 在 *ngFor 内):

//for mappinig item : 
mapLabelValueBS(objet) {
return objet.map(data => {
 return {
   id: data,
   text: data.name
 }
})
}
//this is the one that is causing the problem
<ng-select 
  [allowClear]="true"                                                 
  [items]="mapLabelValueBS(filieres)"
  placeholder="Filière non sélectionné"                                             
  (selected)="selecteFiliere($event)"                                                     
  formControlName="filiere">
</ng-select>

结果在我的页面中(当我单击该字段时,它会自动翻倍):

在此处输入图片说明

没有代码很难知道,但今天我遇到了同样的错误。 原因是我在 FormControl 中确定了一个与 ng-select 要求的数组无关的默认值。 当 FormGroup 加载时,这个错误的默认值被加载到 ng-select 中,错误是selectedItems.map is not a function

几天前我遇到了这个错误,如果你正在绑定一个从后端服务器填充的列表,请确保使用这样的 concat 方法填充列表

   this.userService.getLookup().subscribe((res: any) => {
        this.apps = this.apps.concat(res.data);
    });

当我传递 [items]="value" 值不是数组时出现此错误,因此请检查您是否没有将非数组元素传递给 items 绑定。

您正在尝试绑定对象类型的项目 [items] 属性接受一个数组。 您可以尝试添加管道键值

<ng-select 
  [allowClear]="true"                                                 
  [items]="jsonData | keyvalue"
  placeholder="Filière non sélectionné"                                             
  (selected)="selecteFiliere($event)"                                                     
  formControlName="filiere">
</ng-select>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM