Matoy Matoy - 14 days ago 7
Javascript Question

angularjs - ng-repeat doesn't iterate over hashmap

I am trying to iterate over hashmap like this:

index.html

<div ng-app>
<div ng-controller="ClickToEditCtrl">
<p>Your Data:</p>
<ul ng-repeat="(name, person) in data">
name: {{name}}
--------------------------
age: {{person.age}}
hair: {person.hair}}

</ul>
</div>
</div>


in my ClickToEditCtrl.js

function ClickToEditCtrl($scope) {
$scope.data =[];
$scope.data["Mike"] = new person (11,"blonde");
$scope.data["Dan"] = new person (22, "redhead");
}

function person(age, hair){
this.age = age;
this.hair = hair;
}


that doesn't work..

I am getting empty list.

what am I doning wrong?

thanks!

Answer

You're initializing new array instead of object.
It should be $scope.data = {}; instead of $scope.data =[];

And you'd better put ng-repeat on li rather than ul

<ul>
  <li ng-repeat="(name, person) in data">

    name: {{name}}
    --------------------------
    age: {{person.age}}
    hair: {{person.hair}}
  </li>
</ul>