[英]Angular ng-select : selectedItems.map is not a function
When I'm using ng-select in reactive form angular I get this error:当我以反应形式 angular 使用 ng-select 时,我收到此错误:
ERROR TypeError: selectedItems.map is not a function
错误类型错误:selectedItems.map 不是函数
I have 3 select the first two work very well but in this third one I get the error !我有 3 个选择前两个工作得很好,但在第三个中我得到了错误! to map item i'm using the function (the ng-select is inside *ngFor) :
映射项目我正在使用该函数(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>
the result in my page (when I click on the field it doubles itself) :结果在我的页面中(当我单击该字段时,它会自动翻倍):
Without the code is difficult to know, but today I had the same error.没有代码很难知道,但今天我遇到了同样的错误。 The reason was that I determined a default value in the FormControl that had no relation with the array that ng-select demands.
原因是我在 FormControl 中确定了一个与 ng-select 要求的数组无关的默认值。 When the FormGroup loaded, and this mistaken default was loaded into the ng-select, the error was selectedItems.map is not a function
当 FormGroup 加载时,这个错误的默认值被加载到 ng-select 中,错误是selectedItems.map is not a function
few days ago i came across this error if you are binding a list that is filled from backend server be sure to fill the list using concat method like this几天前我遇到了这个错误,如果你正在绑定一个从后端服务器填充的列表,请确保使用这样的 concat 方法填充列表
this.userService.getLookup().subscribe((res: any) => {
this.apps = this.apps.concat(res.data);
});
当我传递 [items]="value" 值不是数组时出现此错误,因此请检查您是否没有将非数组元素传递给 items 绑定。
You are trying to bind the items of object type.您正在尝试绑定对象类型的项目。 [items] attribute accepts an array.
[items] 属性接受一个数组。 You can trying adding a pipe keyvalue
您可以尝试添加管道键值
<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.