错误: [filter:notarray] ng-options 中的预期数组
Error: [filter:notarray] Expected array in ng-options
我有这个对象。
{
"0":{
"id":"7",
"nombre":"127",
"ciudad":"1"
},
"1":{
"id":"8",
"nombre":"cll 155",
"ciudad":"1"
},
"2":{
"id":"10",
"nombre":"Cra 30",
"ciudad":"1"
},
"3":{
"id":"30",
"nombre":"Repuestos",
"ciudad":"1"
},
"4":{
"id":"6",
"nombre":"Ibagué",
"ciudad":"3"
},
"5":{
"id":"9",
"nombre":"Villavicencio",
"ciudad":"2"
}
}
如果我在ng-options
中使用它,它可以完美运行并创建选项。
<select name="user_dealer"
ng-model="formModel.user_dealer"
ng-options="dealer.nombre for dealer in concesionarios">
<option value="" ng-if="!formModel.user_dealer">Concesionario</option>
</select>
但是,当我添加过滤器时:
<select name="user_dealer"
ng-model="formModel.user_dealer"
ng-options="dealer.nombre for dealer in concesionarios | filter: {ciudad:1}">
<option value="" ng-if="!formModel.user_dealer">Concesionario</option>
</select>
我收到此错误
错误: [filter:notarray] 预期的数组,但收到: {"0":{"id":"7","nombre":" 127","ciudad":"1"},"1":{"id":"8","nombre":"cll 155","ciudad":"1"},"2":{"id":"10","nombre":"Cra 30","ciudad":"1"},"3":{"id":"30","nombre":" Repuestos","ciudad":"1"},"4":{"id":"6","nombre":" Ibagué","ciudad":"3"},"5":{"id":"9","nombre":" Villavicencio","ciudad":"2"}}
您收到错误,因为 ngFilter 需要数组,而不是对象。您可以将concesionarios
对象转换为数组,并使用 HTML 执行以下操作:
<select name="user_dealer"
ng-model="formModel.user_dealer"
ng-options="dealer as dealer.nombre in concesionarios | filter: {ciudad:1}">
<option value="" ng-if="!formModel.user_dealer">Concesionario</option>
</select>
Angular关于过滤器的文档指出:
从数组中选择项的子集,并将其作为新数组返回。
concesionarios
对象不是数组,因此filter
不起作用。concesionarios
对象应如下所示:
[
{
"id":"7",
"nombre":"127",
"ciudad":"1"
},
{
"id":"8",
"nombre":"cll 155",
"ciudad":"1"
},
{
"id":"10",
"nombre":"Cra 30",
"ciudad":"1"
},
... // other objects
]
相关文章:
- 使用filter和map方法将数组中某些元素的第一个字母大写-JavaScript
- AngulsrJS $filter orderby 一个带有 @ 符号的数组键
- 无法按引用角度$filter数组
- 使用“$filter”后选择当前数组项,查看它是否在其中
- d3 本地存储比较数组 .filter 咖啡脚本;数组变量不起作用
- 如何避免在使用$filter后更新原始数组
- 如何在数组上使用 .filter() 只删除“NaN”而不删除字符串和数字
- 使用 .filter 返回数组中的特定元素
- 角度$filter,用于在 JSON 响应中的数组中进行深度过滤
- 为什么我可以在一个不是数组的对象上调用 Array.prototype.filter()
- 使用$filter根据数组中对象的属性进行筛选
- JS Filter函数删除数组中两个ID's在对象中
- Javascript-在不使用Array.prototype.filter的情况下,根据键值获取两个对象数组之间的差异
- rxjs的filter方法没有返回过滤器数组
- 使用filter方法检查数组中每个元素的首字母是否为大写.但我还是得到了整个数组
- 如何在AngularJS中过滤带有动态多个复选框的嵌套对象数组.OR with和Filter
- 基于Angular控制器中的属性,使用$filter对对象数组进行排序
- .filter()数组使用另一个数组's元素
- 使用jQuery's filter()函数提取具有不同条件的数组
- 带有bind()的Javascript数组filter()