Angular Selected - a.forEach 不是一个函数

Angular Chosen - a.forEach is not a function

本文关键字:一个 函数 Selected forEach Angular      更新时间:2023-09-26

使用 Angular Selected 允许国籍的多选下拉列表。https://github.com/localytics/angular-chosen

我收到一个错误

"a.forEach 不是一个函数"

无论您选择了 1、2 还是 0 个选项,都会发生此错误。

我看过这篇文章

获取 a.foreach 不是函数错误

但是我选择的值已经是一个数组,因此它不会提供有关该问题的任何帮助。

这是我的网页

<li class="form-row">
                <span class="label">
                    <label for="nation">Nationality</label>:
                </span>
                <div class="field country">
                    <select 
                    chosen
                    multiple
                    ng-model='person.nation'
                    ng-options='c.name as c.name for c in countries'
                    id="nation" 
                    data-select-max="2" 
                    data-token="Nationalities" 
                    data-placeholder="- Select your nationalities -">
                    </select>
                </div>
            </li>

我的选项($scope.国家)看起来像

[ 
  { id="1",  name="United States"}, 
  { id="2185",  name="Afghanistan"},
  etc....
 ]

任何建议将不胜感激。

数组

对象的属性应该像这样分配:id:"1", name:"United States",而不是id="1"。将等号 (=) 替换为冒号 (:)。

此外,还应按以下方式使用 forEach 函数:

angular.forEach($scope.countries, function(key, value){
// your code here
 });

而不是作为数组的函数:$scope.countries.forEach(...)