获得一个a.foreach不是一个函数错误错误、一个函数、不是、foreach

2023-09-13 04:57:43 作者:以后别挽留别勉强更别将就

我想建立采用了棱角分明的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'
    }]

};