Angular Chosen - a.forEach is not a function

user2085143 picture user2085143 · Jan 14, 2016 · Viewed 13k times · Source

Using Angular Chosen to allow a multi select drop down for nationalities. https://github.com/localytics/angular-chosen

I am getting an error of

"a.forEach is not a function"

This error occurs whether you have selected one, two or zero options.

I have looked at this post

Getting a a.foreach is not a function error

however my selected value is already an array so it does not provide any help on the issue.

Here is my html

<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>

And my options ($scope.countries) looks like

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

Any advice would be much appreciated.

Answer

Ivan Eftimov picture Ivan Eftimov · Jan 14, 2016

The properties of the array objects should be assigned like this: id:"1", name:"United States", instead of id="1". Replace the equal sign (=) with colon (:).

Also you should use forEach function in the following way:

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

and not as a function of the array: $scope.countries.forEach(...)