获得一个a.foreach不是一个函数错误 [英] Getting a a.foreach is not a function error
本文介绍了获得一个a.foreach不是一个函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想建立采用了棱角分明的JS多选名单。我得到一个奇怪的类型错误:a.foreach不是一个功能,我似乎无法弄清楚的时候。
I am trying to build a multiselect list using angular js. I am getting a weird TypeError: a.foreach is not a function and I can’t seem to figure out when.
JS:
var myAppModule = angular.module('multiselect', []);
myAppModule.controller("view", function ($scope) {
$scope.listA = {
values: [{
id: 1,
label: 'aLabel',
subItem: {
name: 'aSubItem'
}
}, {
id: 2,
label: 'bLabel',
subItem: {
name: 'bSubItem'
}
}],
selected: {
name: 'aSubItem'
}
};
})
HTML
<select multiple ng-options="item.subItem as item.label for item in listA.values track by item.id" ng-model="listA.selected"></select>
我不知道我可以做错误的。难道我投什么了吗?
I don’t know what I could be doing wrong. Am I casting something wrong ?
推荐答案
现在的问题是,因为你已经添加了多个
属性选择的值应该是一个数组等等尝试类似于
The problem is since you have added the multiple
attribute the value of the select should be an array so try similar to
$scope.listA = {
values: [{
id: 1,
label: 'aLabel',
subItem: {
name: 'aSubItem'
}
}, {
id: 2,
label: 'bLabel',
subItem: {
name: 'bSubItem'
}
}],
selected: [{
name: 'aSubItem'
}]
};
这篇关于获得一个a.foreach不是一个函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文