Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Assoc Array Foreach

In this AngularJS associative array (array of key,value pairs), ng-repeat is not functioning properly. What's wrong?

function TestCtrl($scope) {
   $scope.list = [];

    $scope.processSomeData = function(){
        $scope.list["testKey"]={};
        $scope.list["testKey"]["test"]="Test value";
    };
    $scope.processSomeData();
}

<div ng-app>
  <h2>Test</h2>
  <div ng-controller="TestCtrl">
        <div ng-repeat="(key,value) in list">
          {{key}}
          {{value.test}}
      </div>
    </div>
</div>

http://jsfiddle.net/ebZkg/

Thanks!

like image 321
Christian Stewart Avatar asked Feb 26 '26 21:02

Christian Stewart


1 Answers

Your "associative array" should be an Javascript Object and not a Javascript Array. You cannot use js Arrays in AngularJS for key, value pairs/maps.

Change your list to an Object and it will work:

$scope.list = {};
like image 179
bmleite Avatar answered Mar 01 '26 10:03

bmleite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!